Skip to content

Commit

Permalink
[ADDED] #637 - unit tests for byRefOp optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
arakov committed Apr 2, 2024
1 parent 2bbd8db commit b8d333e
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 29 deletions.
2 changes: 1 addition & 1 deletion elenasrc3/elc/cliconst.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace elena_lang
{
#define ELC_REVISION_NUMBER 0x0247
#define ELC_REVISION_NUMBER 0x0248

#if defined _M_IX86 || _M_X64

Expand Down
32 changes: 30 additions & 2 deletions elenasrc3/elc/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10028,6 +10028,11 @@ Compiler::Class :: Class(Compiler* compiler, Scope* parent, ref_t reference, Vis
{
}

Compiler::Class :: Class(Namespace& ns, ref_t reference, Visibility visibility)
: compiler(ns.compiler), scope(&ns.scope, reference, visibility)
{
}

void Compiler::Class :: declare(SyntaxNode node)
{
bool extensionDeclaration = isExtensionDeclaration(node);
Expand Down Expand Up @@ -10198,6 +10203,23 @@ void Compiler::Class :: declareClassClass(ClassScope& classClassScope, SyntaxNod
classClassScope.save();
}

// --- Compiler::Method ---

Compiler::Method :: Method(Class& cls)
: compiler(cls.compiler), scope(&cls.scope)
{

}

// --- Compiler::Code ---

Compiler::Code :: Code(Method& method)
: compiler(method.compiler), scope(&method.scope)
{

}


// --- Compiler::Expression ---

Compiler::Expression :: Expression(Compiler* compiler, CodeScope& codeScope, BuildTreeWriter& writer)
Expand All @@ -10206,18 +10228,24 @@ Compiler::Expression :: Expression(Compiler* compiler, CodeScope& codeScope, Bui

}

Compiler::Expression::Expression(Compiler* compiler, SourceScope& symbolScope, BuildTreeWriter& writer)
Compiler::Expression :: Expression(Compiler* compiler, SourceScope& symbolScope, BuildTreeWriter& writer)
: compiler(compiler), scope(&symbolScope), writer(&writer)
{

}

Compiler::Expression::Expression(Symbol& symbol, BuildTreeWriter& writer)
Compiler::Expression :: Expression(Symbol& symbol, BuildTreeWriter& writer)
: compiler(symbol.compiler), scope(&symbol.scope), writer(&writer)
{

}

Compiler::Expression :: Expression(Code& code, BuildTreeWriter& writer)
: compiler(code.compiler), scope(&code.scope), writer(&writer)
{

}

ObjectInfo Compiler::Expression :: compileSymbolRoot(SyntaxNode bodyNode, EAttr mode)
{
writer->appendNode(BuildKey::OpenStatement);
Expand Down
24 changes: 24 additions & 0 deletions elenasrc3/elc/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,29 @@ namespace elena_lang
void declare(SyntaxNode node);

Class(Compiler* compiler, Scope* parent, ref_t reference, Visibility visibility);
Class(Namespace& ns, ref_t reference, Visibility visibility);
};

class Method
{
friend class Compiler;

Compiler* compiler;
MethodScope scope;

public:
Method(Class& cls);
};

class Code
{
friend class Compiler;

Compiler* compiler;
CodeScope scope;

public:
Code(Method& method);
};

class Expression
Expand Down Expand Up @@ -1229,6 +1252,7 @@ namespace elena_lang
ObjectInfo compileSubCode(SyntaxNode node, ExpressionAttribute mode, bool withoutNewScope = false);

Expression(Symbol& symbol, BuildTreeWriter& writer);
Expression(Code& code, BuildTreeWriter& writer);
Expression(Compiler* compiler, CodeScope& codeScope, BuildTreeWriter& writer);
Expression(Compiler* compiler, SourceScope& symbolScope, BuildTreeWriter& writer);
};
Expand Down
9 changes: 8 additions & 1 deletion elenasrc3/elena-tests/bt_optimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ using namespace elena_lang;

// --- BTOptimization1 ---

constexpr auto Declaration1_1 = "namespace (class ( nameattr (identifier \"Object\" ())) singleton (attribute -2147467263 () attribute -2147475455 () attribute -2147479550 () nameattr (identifier \"Struct\" ()) field (attribute -2147475454 () attribute -2147481597 () nameattr (identifier \"_value\" ())dimension (integer \"4\" ())))singleton (attribute -2147467263 ()attribute -2147479546 () nameattr 59 (identifier \"Tester\" ()) script_method (type (identifier \"Struct\" ()) nameattr (identifier \"getValue\" ())code ())))";
constexpr auto Declaration1_1 = "namespace (class ( nameattr (identifier \"Object\" ())) class (attribute -2147467263 () attribute -2147475455 () attribute -2147479550 () nameattr (identifier \"Struct\" ()) field (attribute -2147475454 () attribute -2147481597 () nameattr (identifier \"_value\" ())dimension (integer \"4\" ()))) class ( nameattr (identifier \"TestReference\" ()) field (type (identifier \"Struct\" ()) nameattr (identifier \"_value\" ())) ) class (attribute -2147467263 ()attribute -2147479546 () nameattr (identifier \"Tester\" ()) method (type (identifier \"Struct\" ()) nameattr (identifier \"getValue\" ())code ())))";
constexpr auto SyntaxTree1_1 = "expression(assign_operation(object(type(identifier \"Struct\"())identifier \"r\"())expression(message_operation(object(identifier \"Tester\"())message(identifier \"getValue\" ())))))";
constexpr auto BuildTree1_1 = "byrefmark -8 () local_address -8 () saving_stack 1 () class_reference 59 () saving_stack () argument () direct_call_op 18434 (type 59 ()) local_address -8 () copying -4 (size 4 ())";

void BTOptimization1_1 :: SetUp()
{
SyntaxTreeWriter writer(syntaxTree);
writer.appendNode(SyntaxKey::Root);

BuildTreeWriter buildWriter(buildTree);
buildWriter.appendNode(BuildKey::Root);

declarationNode = syntaxTree.readRoot().appendChild(SyntaxKey::Idle, 1);
exprNode = syntaxTree.readRoot().appendChild(SyntaxKey::Idle, 2);

SyntaxTreeSerializer::load(Declaration1_1, declarationNode);
SyntaxTreeSerializer::load(SyntaxTree1_1, exprNode);

auto outputNode = buildTree.readRoot();
BuildTreeSerializer::load(BuildTree1_1, outputNode);
}
38 changes: 23 additions & 15 deletions elenasrc3/elena-tests/compiler_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ using namespace elena_lang;

TEST_F(BTOptimization1_1, CompilerTest)
{
//// Arrange
//ModuleScopeBase* moduleScope = env.createModuleScope(true, false);
//Compiler* compiler = env.createCompiler();
// Arrange
ModuleScopeBase* moduleScope = env.createModuleScope(true, false);
moduleScope->buildins.superReference = 1;
moduleScope->buildins.wrapperTemplateReference = 3;

//BuildTree output;
//BuildTreeWriter writer(output);
//Compiler::Namespace nsScope(compiler, moduleScope, nullptr, nullptr, nullptr);
Compiler* compiler = env.createCompiler();

//// Act
//nsScope.declare(declarationNode.firstChild(), true);
BuildTree output;
BuildTreeWriter writer(output);
Compiler::Namespace nsScope(compiler, moduleScope, nullptr, nullptr, nullptr);

//Compiler::Symbol symbol(nsScope, 0, Visibility::Internal);
//Compiler::Expression expression(symbol, writer);
//expression.compileRoot(exprNode.firstChild(), ExpressionAttribute::None);
Compiler::Class cls(nsScope, 0, Visibility::Internal);
Compiler::Method method(cls);
Compiler::Code code(method);

//// Assess
bool matched = /*BuildTree::compare(buildTree.readRoot(), output.readRoot())*/true;
// Act
nsScope.declare(declarationNode.firstChild(), true);

writer.newNode(BuildKey::Tape);
Compiler::Expression expression(code, writer);
expression.compileRoot(exprNode.firstChild(), ExpressionAttribute::NoDebugInfo);
writer.closeNode();

// Assess
bool matched = BuildTree::compare(buildTree.readRoot(), output.readRoot(), true);
EXPECT_TRUE(matched);

//freeobj(compiler);
//freeobj(moduleScope);
freeobj(compiler);
freeobj(moduleScope);
}
42 changes: 37 additions & 5 deletions elenasrc3/elena-tests/tests_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ ref_t TestModuleScope :: mapTemplateIdentifier(ustr_t ns, ustr_t identifier, Vis

ref_t TestModuleScope :: mapFullReference(ustr_t referenceName, bool existing)
{
return 0;
if (emptystr(referenceName))
return 0;

return module->mapReference(referenceName, existing);
}

ref_t TestModuleScope :: mapWeakReference(ustr_t referenceName, bool existing)
Expand Down Expand Up @@ -166,6 +169,38 @@ Visibility TestModuleScope :: retrieveVisibility(ref_t reference)
return Visibility::Private;
}

// --- TestTemplateProssesor ---

ref_t TestTemplateProssesor :: generateClassTemplate(ModuleScopeBase& moduleScope, ustr_t ns, ref_t templateRef,
List<SyntaxNode>& parameters, bool declarationMode, ExtensionMap* outerExtensionList)
{
return templateRef;
}

bool TestTemplateProssesor :: importTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& parameters)
{
return false;
}

bool TestTemplateProssesor :: importInlineTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& parameters)
{
return false;
}

bool TestTemplateProssesor :: importPropertyTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& parameters)
{
return false;
}

bool TestTemplateProssesor :: importCodeTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& arguments, List<SyntaxNode>& parameters)
{
return false;
}

// --- CompilerEnvironment ---

CompilerEnvironment :: CompilerEnvironment()
Expand All @@ -177,15 +212,12 @@ ModuleScopeBase* CompilerEnvironment :: createModuleScope(bool tapeOptMode, bool
{
auto scope = new TestModuleScope(tapeOptMode, threadFriendly);

// by default - the first reference is a super class
scope->buildins.superReference = 1;

return scope;
}

Compiler* CompilerEnvironment :: createCompiler()
{
auto compiler = new Compiler(nullptr, nullptr, nullptr, CompilerLogic::getInstance());
auto compiler = new Compiler(nullptr, nullptr, TestTemplateProssesor::getInstance(), CompilerLogic::getInstance());

compiler->setNoValidation();

Expand Down
23 changes: 23 additions & 0 deletions elenasrc3/elena-tests/tests_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ namespace elena_lang
TestModuleScope(bool tapeOptMode, bool threadFriendly);
};

class TestTemplateProssesor : public TemplateProssesorBase
{
public:
ref_t generateClassTemplate(ModuleScopeBase& moduleScope, ustr_t ns, ref_t templateRef,
List<SyntaxNode>& parameters, bool declarationMode, ExtensionMap* outerExtensionList) override;

bool importTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& parameters) override;
bool importInlineTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& parameters) override;
bool importPropertyTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& parameters) override;
bool importCodeTemplate(ModuleScopeBase& moduleScope, ref_t templateRef, SyntaxNode target,
List<SyntaxNode>& arguments, List<SyntaxNode>& parameters) override;

static TemplateProssesorBase* getInstance()
{
static TestTemplateProssesor instance;

return &instance;
}
};

// --- CompilerEnvironment ---
class CompilerEnvironment
{
Expand Down
32 changes: 30 additions & 2 deletions elenasrc3/engine/buildtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,22 @@ namespace elena_lang
class BuildTree : public Tree<BuildKey, BuildKey::None>
{
public:
static bool compare(Tree<BuildKey, BuildKey::None>::Node n1, Tree<BuildKey, BuildKey::None>::Node n2)
static bool compare(Tree<BuildKey, BuildKey::None>::Node n1, Tree<BuildKey, BuildKey::None>::Node n2, bool onlyChildren)
{
// !! temporal
if (onlyChildren || (n1.key == n2.key && n1.arg.value == n2.arg.value)) {
Tree<BuildKey, BuildKey::None>::Node c1 = n1.firstChild();
Tree<BuildKey, BuildKey::None>::Node c2 = n2.firstChild();
while (c1.key != BuildKey::None) {
if (!compare(c1, c2, false))
return false;

c1 = c1.nextNode();
c2 = c2.nextNode();
}

return c2.key == BuildKey::None;
}

return false;
}

Expand Down Expand Up @@ -286,6 +299,21 @@ namespace elena_lang
map.add("direct_call_op", BuildKey::DirectCallOp);
map.add("addingint", BuildKey::AddingInt);
map.add("saving_index", BuildKey::SavingIndex);
map.add("open_frame", BuildKey::OpenFrame);
map.add("close_frame", BuildKey::CloseFrame);
map.add("argument", BuildKey::Argument);
map.add("class_reference", BuildKey::ClassReference);
map.add("open_statement", BuildKey::OpenStatement);
map.add("end_statement", BuildKey::EndStatement);

map.add("tape", BuildKey::Tape);
map.add("type", BuildKey::Type);
map.add("size", BuildKey::Size);
map.add("method_name", BuildKey::MethodName);
map.add("arguments_info", BuildKey::ArgumentsInfo);
map.add("variable_info", BuildKey::VariableInfo);
map.add("column", BuildKey::Column);
map.add("row", BuildKey::Row);
}
};

Expand Down
Loading

0 comments on commit b8d333e

Please sign in to comment.