simple gc

This commit is contained in:
2023-12-23 19:42:21 +01:00
parent a9050fc9b9
commit 0ccb7e9b56
8 changed files with 145 additions and 22 deletions

66
.clang-format Normal file
View File

@@ -0,0 +1,66 @@
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 0
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never

7
.idea/codeStyles/Project.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<clangFormatSettings>
<option name="ENABLED" value="true" />
</clangFormatSettings>
</code_scheme>
</component>

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

View File

@@ -5,8 +5,8 @@
#ifndef PSIL_CELL_H #ifndef PSIL_CELL_H
#define PSIL_CELL_H #define PSIL_CELL_H
#include <cstdint>
#include <cassert> #include <cassert>
#include <cstdint>
enum class CellType { enum class CellType {
INT, INT,
@@ -19,6 +19,8 @@ struct Cell {
virtual ~Cell() = 0; virtual ~Cell() = 0;
CellType _type; CellType _type;
bool live = false;
}; };
struct IntCell : public Cell { struct IntCell : public Cell {
@@ -60,7 +62,6 @@ struct CommandCell : public IntCell {
assert((_val > 0 && static_cast<CommandNum>(_val) <= CommandNum::END)); assert((_val > 0 && static_cast<CommandNum>(_val) <= CommandNum::END));
return static_cast<CommandNum>(_val); return static_cast<CommandNum>(_val);
} }
}; };
struct ConsCell : public Cell { struct ConsCell : public Cell {
@@ -74,4 +75,4 @@ struct ConsCell : public Cell {
Cell *_cdr = nullptr; Cell *_cdr = nullptr;
}; };
#endif //PSIL_CELL_H #endif//PSIL_CELL_H

View File

@@ -5,9 +5,10 @@
#ifndef PSIL_VM_H #ifndef PSIL_VM_H
#define PSIL_VM_H #define PSIL_VM_H
#include <vector>
#include<utility>
#include <iostream> #include <iostream>
#include <list>
#include <utility>
#include <vector>
#include "Cell.h" #include "Cell.h"
@@ -19,10 +20,10 @@ public:
void step(); void step();
// template<typename T> // template<typename T>
// void appendCommand(T cell) { // void appendCommand(T cell) {
// push(_c, makeCell<T>(std::move(cell))); // push(_c, makeCell<T>(std::move(cell)));
// } // }
template<typename T> template<typename T>
void appendCommand(T *cell) { void appendCommand(T *cell) {
@@ -57,8 +58,10 @@ public:
return what; return what;
} }
uint64_t cellCount() const;
private: private:
std::vector<Cell *> _cells; std::list<Cell *> _cells;
ConsCell *_s = nullptr; ConsCell *_s = nullptr;
ConsCell *_e = nullptr; ConsCell *_e = nullptr;
ConsCell *_c = nullptr; ConsCell *_c = nullptr;
@@ -67,7 +70,9 @@ private:
std::istream &_instream; std::istream &_instream;
std::ostream &_outstream; std::ostream &_outstream;
void gc();
}; };
#endif //PSIL_VM_H #endif//PSIL_VM_H

View File

@@ -2,10 +2,11 @@
// Created by Stepan Usatiuk on 22.12.2023. // Created by Stepan Usatiuk on 22.12.2023.
// //
#include "../includes/VM.h" #include "VM.h"
#include <utility>
#include <iostream> #include <iostream>
#include <set>
#include <utility>
void VM::run() { void VM::run() {
while (!_stop) step(); while (!_stop) step();
@@ -113,7 +114,7 @@ void VM::step() {
_s = s; _s = s;
push(_s, ret); push(_s, ret);
gc();
break; break;
} }
case CommandCell::CommandNum::DUM: { case CommandCell::CommandNum::DUM: {
@@ -143,6 +144,7 @@ void VM::step() {
} }
case CommandCell::CommandNum::STOP: { case CommandCell::CommandNum::STOP: {
_stop = true; _stop = true;
gc();
break; break;
} }
case CommandCell::CommandNum::ADD: { case CommandCell::CommandNum::ADD: {
@@ -188,7 +190,46 @@ void VM::step() {
default: default:
assert(false); assert(false);
} }
} }
VM::VM(std::istream &instream, std::ostream &outstream) : _instream(instream), _outstream(outstream) {} VM::VM(std::istream &instream, std::ostream &outstream) : _instream(instream), _outstream(outstream) {}
void VM::gc() {
std::function<void(ConsCell *)> visit = [&](ConsCell *c) {
if (c == nullptr) return;
if (c->live) return;
c->live = true;
if (c->_car) {
if (c->_car->_type == CellType::CONS) visit(dynamic_cast<ConsCell *>(c->_car));
c->_car->live = true;
}
if (c->_cdr) {
if (c->_cdr->_type == CellType::CONS) visit(dynamic_cast<ConsCell *>(c->_cdr));
c->_cdr->live = true;
}
};
visit(_s);
visit(_e);
visit(_c);
visit(_d);
uint64_t freed = 0;
_cells.remove_if([&](Cell *l) {
bool ret = !l->live;
if (ret) {
freed += 1;
delete l;
} else {
l->live = false;
}
return ret;
});
std::cout << "GC Freed " << freed << std::endl;
}
uint64_t VM::cellCount() const {
return _cells.size();
}

View File

@@ -91,7 +91,6 @@ TEST(VMTest, SimpleFunction) {
} }
ssout.flush(); ssout.flush();
EXPECT_EQ(ssout.str(), "3"); EXPECT_EQ(ssout.str(), "3");
} }
TEST(VMTest, RecursiveFunction) { TEST(VMTest, RecursiveFunction) {
@@ -171,7 +170,7 @@ TEST(VMTest, RecursiveFunction) {
vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
vm.push(fibcallfn, vm.makeCell<IntCell>(20)); vm.push(fibcallfn, vm.makeCell<IntCell>(10));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
@@ -236,6 +235,5 @@ TEST(VMTest, RecursiveFunction) {
vm.run(); vm.run();
} }
ssout.flush(); ssout.flush();
EXPECT_EQ(ssout.str(), "2358 6765"); EXPECT_EQ(ssout.str(), "2358 55");
} }

View File

@@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "VM.h"
#include "Parser.h" #include "Parser.h"
#include "VM.h"
TEST(VMWithParserTest, BasicHello) { TEST(VMWithParserTest, BasicHello) {
std::stringstream ssin; std::stringstream ssin;
@@ -50,9 +50,9 @@ TEST(VMWithParserTest, RecFunction) {
VM vm(ssin, ssout); VM vm(ssin, ssout);
Parser parser(vm); Parser parser(vm);
parser.loadSecd( parser.loadSecd(
"( DUM NIL LDF ( LD ( 1 . 1 ) SEL ( LD ( 1 . 1 ) LDC -1 ADD SEL ( NIL LD ( 1 . 1 ) LDC -1 ADD CONS LD ( 2 . 1 ) AP NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN ) RET ) CONS LDF ( NIL LDC 20 CONS LD ( 1 . 1 ) AP RET ) RAP PUTNUM STOP )"); "( DUM NIL LDF ( LD ( 1 . 1 ) SEL ( LD ( 1 . 1 ) LDC -1 ADD SEL ( NIL LD ( 1 . 1 ) LDC -1 ADD CONS LD ( 2 . 1 ) AP NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN ) RET ) CONS LDF ( NIL LDC 10 CONS LD ( 1 . 1 ) AP RET ) RAP PUTNUM STOP )");
vm.run(); vm.run();
} }
ssout.flush(); ssout.flush();
EXPECT_EQ(ssout.str(), "6765"); EXPECT_EQ(ssout.str(), "55");
} }