Skip to content

Commit

Permalink
Add basic AST expression-evaluation support
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Ballance <matt.ballance@gmail.com>
  • Loading branch information
mballance committed Nov 6, 2023
1 parent 2e8e485 commit 429bd08
Show file tree
Hide file tree
Showing 31 changed files with 1,409 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "MarkerCollector.h"
#include "NameResolver.h"
#include "TaskFindElementByLocation.h"
#include "ValInt.h"
#include "pss_stdlib.h"


Expand Down Expand Up @@ -108,6 +109,13 @@ ITaskFindElementByLocation *Factory::mkTaskFindElementByLocation() {
return new TaskFindElementByLocation(m_dmgr);
}

IValInt *Factory::mkValInt(
bool is_signed,
int32_t width,
int64_t init) {
return new ValInt(is_signed, width, init);
}

IFactory *Factory::inst() {
if (!m_inst) {
m_inst = FactoryUP(new Factory());
Expand Down
5 changes: 5 additions & 0 deletions src/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Factory : public virtual IFactory {

virtual ITaskFindElementByLocation *mkTaskFindElementByLocation() override;

virtual IValInt *mkValInt(
bool is_signed,
int32_t width,
int64_t init=0) override;

static IFactory *inst();

private:
Expand Down
9 changes: 6 additions & 3 deletions src/TaskCompareParamLists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ namespace zsp {
namespace parser {


TaskCompareParamLists::TaskCompareParamLists(dmgr::IDebugMgr *dmgr) {
DEBUG_INIT("TaskCompareParamLists", dmgr);
TaskCompareParamLists::TaskCompareParamLists(IFactory *factory) :
m_tref_comp(factory) {
DEBUG_INIT("TaskCompareParamLists", factory->getDebugMgr());

}

Expand Down Expand Up @@ -71,7 +72,9 @@ bool TaskCompareParamLists::equal(

// How do we compare?
if (type_value[0]) {
type_value[0]->getDflt()->accept(m_this);
ret = !m_tref_comp.equal(
type_value[0]->getDflt(),
type_value[1]->getDflt());
} else if (expr_value[0] && expr_value[0]->getDflt()) {
expr_value[0]->getDflt()->accept(m_this);
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/TaskCompareParamLists.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
#pragma once
#include "dmgr/IDebugMgr.h"
#include "zsp/ast/impl/VisitorBase.h"
#include "zsp/parser/IFactory.h"
#include "TaskCompareTypeRefs.h"

namespace zsp {
namespace parser {

class TaskCompareParamLists : public ast::VisitorBase {
public:
TaskCompareParamLists(dmgr::IDebugMgr *dmgr);
TaskCompareParamLists(IFactory *factory);

virtual ~TaskCompareParamLists();

Expand Down Expand Up @@ -57,6 +59,7 @@ class TaskCompareParamLists : public ast::VisitorBase {
ast::ITemplateValueParamDecl *m_expr_value;
const ast::ITemplateParamDeclList *m_plist1;
const ast::ITemplateParamDeclList *m_plist2;
TaskCompareTypeRefs m_tref_comp;

};

Expand Down
123 changes: 123 additions & 0 deletions src/TaskCompareTypeRefs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* TaskCompareTypeRefs.cpp
*
* Copyright 2023 Matthew Ballance and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on:
* Author:
*/
#include "dmgr/impl/DebugMacros.h"
#include "zsp/parser/IVal.h"
#include "zsp/parser/IValInt.h"
#include "TaskCompareTypeRefs.h"
#include "TaskCompareVal.h"


namespace zsp {
namespace parser {


TaskCompareTypeRefs::TaskCompareTypeRefs(IFactory *factory) :
m_expr_eval(factory), m_comp_val(factory->getDebugMgr()) {
DEBUG_INIT("zsp::parser::TaskCompareTypeRefs", factory->getDebugMgr());
}

TaskCompareTypeRefs::~TaskCompareTypeRefs() {

}

bool TaskCompareTypeRefs::equal(
ast::IDataType *tref1,
ast::IDataType *tref2) {
DEBUG_ENTER("equal");
bool ret = true;
ast::IDataTypeInt *type_int = 0;
ast::IDataTypeString *type_str = 0;

m_type_int = 0;
m_type_int = 0;
tref1->accept(m_this);
type_int = m_type_int;
type_str = m_type_str;

m_type_int = 0;
m_type_int = 0;
tref2->accept(m_this);

if (m_type_int && type_int) {
// Both are type int
IVal *w1 = m_expr_eval.eval(m_type_int->getWidth());
IVal *w2 = m_expr_eval.eval(type_int->getWidth());
DEBUG("Both are type 'int'");
DEBUG("T1: width=%d signed=%d", m_type_int->getWidth(), m_type_int->getIs_signed());
DEBUG("T2: width=%d signed=%d", type_int->getWidth(), type_int->getIs_signed());
ret &= m_comp_val.equal(w1, w2);
} else if (m_type_str && type_str) {
DEBUG("Both are type 'str'");
// Nothing else to do...
} else {
ret = false;
}
DEBUG_LEAVE("equal %d", ret);
return ret;
}

void TaskCompareTypeRefs::visitExprRefPath(ast::IExprRefPath *i) {
DEBUG_ENTER("visitExprRefPath");

DEBUG_LEAVE("visitExprRefPath");
}

void TaskCompareTypeRefs::visitExprRefPathContext(ast::IExprRefPathContext *i) {
DEBUG_ENTER("visitExprRefPathContext");

DEBUG_LEAVE("visitExprRefPathContext");
}

void TaskCompareTypeRefs::visitExprRefPathStatic(ast::IExprRefPathStatic *i) {
DEBUG_ENTER("visitExprRefPathStatic");

DEBUG_LEAVE("visitExprRefPathStatic");
}


void TaskCompareTypeRefs::visitDataTypeInt(ast::IDataTypeInt *i) {
DEBUG_ENTER("visitDataTypeInt");
m_type_int = i;
DEBUG_LEAVE("visitDataTypeInt");
}

void TaskCompareTypeRefs::visitDataTypeRef(ast::IDataTypeRef *i) {
DEBUG_ENTER("visitDataTypeRef");

DEBUG_LEAVE("visitDataTypeRef");
}

void TaskCompareTypeRefs::visitDataTypeString(ast::IDataTypeString *i) {
DEBUG_ENTER("visitDataTypeString");
m_type_str = i;
DEBUG_LEAVE("visitDataTypeString");
}

void TaskCompareTypeRefs::visitDataTypeUserDefined(ast::IDataTypeUserDefined *i) {
DEBUG_ENTER("visitDataTypeUserDefined");

DEBUG_LEAVE("visitDataTypeUserDefined");
}

dmgr::IDebug *TaskCompareTypeRefs::m_dbg = 0;

}
}
70 changes: 70 additions & 0 deletions src/TaskCompareTypeRefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* TaskCompareTypeRefs.h
*
* Copyright 2023 Matthew Ballance and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on:
* Author:
*/
#pragma once
#include "dmgr/IDebugMgr.h"
#include "zsp/ast/impl/VisitorBase.h"
#include "zsp/parser/IFactory.h"
#include "TaskEvalExpr.h"
#include "TaskCompareVal.h"

namespace zsp {
namespace parser {



class TaskCompareTypeRefs : public virtual ast::VisitorBase {
public:
TaskCompareTypeRefs(IFactory *factory);

virtual ~TaskCompareTypeRefs();

bool equal(
ast::IDataType *tref1,
ast::IDataType *tref2);

virtual void visitExprRefPath(ast::IExprRefPath *i) override;

virtual void visitExprRefPathContext(ast::IExprRefPathContext *i) override;

virtual void visitExprRefPathStatic(ast::IExprRefPathStatic *i) override;

virtual void visitDataTypeInt(ast::IDataTypeInt *i) override;

virtual void visitDataTypeRef(ast::IDataTypeRef *i) override;

virtual void visitDataTypeString(ast::IDataTypeString *i) override;

virtual void visitDataTypeUserDefined(ast::IDataTypeUserDefined *i) override;


private:
static dmgr::IDebug *m_dbg;
TaskEvalExpr m_expr_eval;
TaskCompareVal m_comp_val;
ast::IDataTypeInt *m_type_int;
ast::IDataTypeString *m_type_str;

};

}
}


77 changes: 77 additions & 0 deletions src/TaskCompareVal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* TaskCompareVal.cpp
*
* Copyright 2023 Matthew Ballance and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on:
* Author:
*/
#include "dmgr/impl/DebugMacros.h"
#include "TaskCompareVal.h"


namespace zsp {
namespace parser {


TaskCompareVal::TaskCompareVal(dmgr::IDebugMgr *dmgr) {
DEBUG_INIT("zsp::parser::TaskCompareVal", dmgr);
}

TaskCompareVal::~TaskCompareVal() {

}

bool TaskCompareVal::equal(IVal *v1, IVal *v2) {
DEBUG_ENTER("equal");
m_ret = true;

if (!v1 || !v2) {
m_ret = false;
} else if (v1->getKind() != v2->getKind()) {
m_ret = false;
} else {
m_val2 = v2;
v1->accept(this);
}

DEBUG_LEAVE("equal %d", m_ret);
return m_ret;
}

void TaskCompareVal::visitValInt(IValInt *v) {
DEBUG_ENTER("visitValInt");
if (m_val2->getKind() != ValKind::Int) {
DEBUG("Unequal values: v_val2::kind=%d", m_val2->getKind());
m_ret = false;
} else {
IValInt *v2 = dynamic_cast<IValInt *>(m_val2);
if (!v->isSigned() && !v2->isSigned()) {
// Perform unsigned comparison
DEBUG("v1=%llu v2=%llu", v->getValS(), v2->getValU());
m_ret &= (v->getValU() == v2->getValU());
} else {
// Signed comparison
DEBUG("v1=%lld v2=%lld", v->getValS(), v2->getValU());
m_ret &= (v->getValS() == v2->getValS());
}
}
DEBUG_LEAVE("visitValInt ret=%d", m_ret);
}

dmgr::IDebug *TaskCompareVal::m_dbg = 0;

}
}
Loading

0 comments on commit 429bd08

Please sign in to comment.