Skip to content

Commit

Permalink
Optimize memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillMagic committed Mar 22, 2018
1 parent ce4f0fc commit 82b4b5b
Show file tree
Hide file tree
Showing 15 changed files with 590 additions and 428 deletions.
22 changes: 11 additions & 11 deletions include/compile.h
Expand Up @@ -5,24 +5,24 @@
#include "runtime/function.h"
#include "runtime/environment.h"
#include "virtualmachine.h"
#include "parse.h"

namespace CVM
{
namespace Compile
class Compiler
{
class Compiler
{
public:
explicit Compiler() = default;

private:
public:
explicit Compiler() {}

};
bool compile(ParseInfo &parseinfo, Runtime::FuncTable &functable);


Runtime::Instruction Compile(const InstStruct::Instruction &inst, const InstStruct::Function &func);
Runtime::InstFunction Compile(const InstStruct::Function &func);
private:
Runtime::Instruction compile(const InstStruct::Instruction &inst, const FunctionInfo &info);
Runtime::InstFunction compile(const InstStruct::Function &func);
};

namespace Compile
{
Runtime::LocalEnvironment* CreateLoaclEnvironment(const Runtime::InstFunction &func, const TypeInfoMap &tim);
Runtime::GlobalEnvironment* CreateGlobalEnvironment(Config::RegisterIndexType dysize, const TypeInfoMap &tim, const LiteralDataPool &datasmap, const Runtime::FuncTable &functable);
}
Expand Down
54 changes: 54 additions & 0 deletions include/funcinfo.h
@@ -0,0 +1,54 @@
#pragma once
#include "../prilib/include/lightlist.h"
#include "config.h"
#include "typeinfo.h"

namespace CVM
{
struct FunctionInfo
{
public:
using ArgList = PriLib::lightlist<Config::RegisterIndexType>;
using TypeList = PriLib::lightlist<TypeIndex>;

public:
FunctionInfo() = default;
FunctionInfo(const FunctionInfo &info) = default;

explicit FunctionInfo(Config::RegisterIndexType &&dysize, TypeList &&stvarb_typelist, ArgList &&arglist) :
_dyvarb_count(std::move(dysize)),
stvarb_typelist(std::move(stvarb_typelist)),
arglist(std::move(arglist)) {}

explicit FunctionInfo(FunctionInfo &&info) :
_dyvarb_count(std::move(info._dyvarb_count)),
stvarb_typelist(std::move(info.stvarb_typelist)),
arglist(std::move(info.arglist)) {}

public:
Config::RegisterIndexType _dyvarb_count = 0;
TypeList stvarb_typelist;
ArgList arglist;

public:
bool is_dyvarb(Config::RegisterIndexType index) const {
return Config::is_dynamic(index, dyvarb_count(), stvarb_count());
}
bool is_stvarb(Config::RegisterIndexType index) const {
return Config::is_static(index, dyvarb_count(), stvarb_count());
}
TypeIndex get_stvarb_type(Config::RegisterIndexType index) const {
return stvarb_typelist.at(Config::get_static_id(index, dyvarb_count(), stvarb_count()));
}

Config::RegisterIndexType dyvarb_count() const {
return _dyvarb_count;
}
Config::RegisterIndexType stvarb_count() const {
return Config::convertToRegisterIndexType(stvarb_typelist.size());
}
Config::RegisterIndexType regsize() const {
return dyvarb_count() + stvarb_count();
}
};
}
47 changes: 17 additions & 30 deletions include/inststruct/function.h
@@ -1,46 +1,33 @@
#pragma once
#include "functioninfo.h"
#include "funcinfo.h"
#include "config.h"
#include "instruction.h"

namespace CVM
{
namespace InstStruct
{
class Function
using InstList = std::vector<Instruction*>; // TODO

struct Function : public FunctionInfo
{
public:
explicit Function(FunctionInfo &&info)
: _info(std::move(info)) {}
explicit Function() = default;
explicit Function(const Function &info) = default;

const InstList& instlist() const {
return _info.instdata;
}
explicit Function(Function &&info)
: instdata(info.instdata), FunctionInfo(std::move(info)) {}

bool is_dyvarb(Config::RegisterIndexType index) const {
return Config::is_dynamic(index, dyvarb_count(), stvarb_count());
}
bool is_stvarb(Config::RegisterIndexType index) const {
return Config::is_static(index, dyvarb_count(), stvarb_count());
}
TypeIndex get_stvarb_type(Config::RegisterIndexType index) const {
return _info.stvarb_typelist.at(Config::get_static_id(index, dyvarb_count(), stvarb_count()));
}
const TypeList& stvarb_typelist() const {
return _info.stvarb_typelist;
}
const ArgList& arglist() const {
return _info.arglist;
}
explicit Function(InstList &&il, Config::RegisterIndexType &&dysize, TypeList &&stvarb_typelist, ArgList &&arglist) :
instdata(std::move(il)),
FunctionInfo(std::move(dysize), std::move(stvarb_typelist), std::move(arglist)) {}

Config::RegisterIndexType dyvarb_count() const {
return _info.dyvarb_count;
}
Config::RegisterIndexType stvarb_count() const {
return Config::convertToRegisterIndexType(_info.stvarb_typelist.size());
~Function() {
for (auto &p : instdata)
if (p)
delete p;
}

private:
FunctionInfo _info;
InstList instdata;
};
}
}
42 changes: 0 additions & 42 deletions include/inststruct/functioninfo.h

This file was deleted.

63 changes: 63 additions & 0 deletions include/inststruct/identkeytable.h
@@ -0,0 +1,63 @@
#pragma once
#include "basic.h"
#include "function.h"

namespace CVM
{
namespace InstStruct
{
struct IdentKeyTable
{
using FuncPtr = std::shared_ptr<InstStruct::Function>;

bool hasKey(const std::string &key) {
return keytable.find(key) != keytable.end();
}
bool insert(const std::string &key) {
if (!hasKey(key)) {
_insert(key);
return true;
}
return false;
}
size_t getID(const std::string &key) {
auto iter = keytable.find(key);
if (iter == keytable.end()) {
return _insert(key);
}
return iter->second;
}
auto& getData(size_t id) {
return functable.at(id);
}
auto& getData(const std::string &key) {
return getData(getID(key));
}
/*auto begin() const {
return functable.begin();
}
auto end() const {
return functable.end();
}*/

template <typename _FTy>
void each(_FTy f) {
for (auto pair : keytable) {
f(pair.second, functable.at(pair.second));
}
}

private:
std::map<std::string, size_t> keytable;
std::vector<FuncPtr> functable;

size_t _insert(const std::string &key) {
size_t id = keytable.size();
keytable.insert({ key, id });
while (functable.size() <= id)
functable.push_back(nullptr);
return id;
}
};
}
}
5 changes: 2 additions & 3 deletions include/inststruct/instpart.h
Expand Up @@ -170,7 +170,7 @@ namespace CVM
struct Identifier
{
public:
using Type = std::string;
using Type = size_t;

explicit Identifier(Type index)
: _data(index) {}
Expand All @@ -180,8 +180,7 @@ namespace CVM
}

std::string toString() const {
return _data;
//return "i" + std::to_string(_index);
return "i" + std::to_string(_data);
}

private:
Expand Down
2 changes: 2 additions & 0 deletions include/inststruct/instruction.h
Expand Up @@ -10,6 +10,8 @@ namespace CVM
explicit Instruction(InstCode ic, uint8_t id = 0)
: instcode(ic), _subid(id) {}

virtual ~Instruction() {}

InstCode instcode;
uint8_t _subid;
};
Expand Down
5 changes: 2 additions & 3 deletions include/parse.h
Expand Up @@ -5,6 +5,7 @@
#include "typeinfo.h"
#include "inststruct/function.h"
#include "inststruct/instpart.h"
#include "inststruct/identkeytable.h"
#include "datapool.h"

namespace CVM
Expand All @@ -14,10 +15,8 @@ namespace CVM
PriLib::StorePtr<ParseInfo> createParseInfo(TypeInfoMap &tim);
void parseFile(ParseInfo &parseinfo, PriLib::TextFile &file);

using FunctionSet = std::map<std::string, InstStruct::Function*>;
FunctionSet createFunctionSet(ParseInfo &parseinfo);

std::string getEntry(ParseInfo &parseinfo);
LiteralDataPoolCreater& getDataSectionMap(ParseInfo &parseinfo);
InstStruct::IdentKeyTable& getFunctionTable(ParseInfo &parseinfo);
bool haveError(const ParseInfo &parseinfo);
}
2 changes: 1 addition & 1 deletion include/runtime/functable.h
Expand Up @@ -6,6 +6,6 @@ namespace CVM
{
namespace Runtime
{
using FuncTable = PriLib::BijectionMap<std::string, Function*>;
using FuncTable = std::map<size_t, Function*>;
}
}
16 changes: 9 additions & 7 deletions include/runtime/function.h
Expand Up @@ -3,11 +3,10 @@
#include <vector>
#include "instruction.h"
#include "datapointer.h"
#include "funcinfo.h"

namespace CVM
{
namespace InstStruct { class Function; }

namespace Runtime
{
enum FunctionType
Expand All @@ -20,6 +19,8 @@ namespace CVM
class Function
{
public:
virtual ~Function() {}

virtual FunctionType type() const {
return ft_null;
}
Expand All @@ -28,10 +29,11 @@ namespace CVM
class InstFunction : public Function
{
public:
using Info = FunctionInfo;
using InstList = std::vector<Instruction>;
public:
explicit InstFunction(const InstList &il, const InstStruct::Function *ifunc)
: _data(il), _instfunc(ifunc) {}
explicit InstFunction(const InstList &il, const Info &info)
: _data(il), _info(info) {}

virtual FunctionType type() const {
return ft_inst;
Expand All @@ -53,13 +55,13 @@ namespace CVM
return _data;
}

const InstStruct::Function& instfunc() const {
return *_instfunc;
const Info& info() const {
return _info;
}

private:
InstList _data;
const InstStruct::Function *_instfunc;
Info _info;
};

class PointerFunction : public Function
Expand Down

0 comments on commit 82b4b5b

Please sign in to comment.