Skip to content

Commit

Permalink
work on AST to RAM, change lat binary syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
QXG2 committed Nov 25, 2019
1 parent 19ee54d commit 4b03ac2
Show file tree
Hide file tree
Showing 15 changed files with 497 additions and 476 deletions.
12 changes: 2 additions & 10 deletions src/AstLatticeBinaryFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,9 @@ class AstLatticeBinaryFunction: public AstNode {
os << "{\n";
for (auto& pm : pairmaps) {
os << "(";
if (pm.first != nullptr) {
pm.first->print(os);
} else {
os << "_";
}
pm.first->print(os);
os << ",";
if (pm.second != nullptr) {
pm.second->print(os);
} else {
os << "_";
}
pm.second->print(os);
os << ")=>";
pm.output->print(os);
os << "\n";
Expand Down
72 changes: 36 additions & 36 deletions src/AstTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,13 @@ std::unique_ptr<RamLatticeAssociation> AstTranslator::translateLatticeAssoc(
const SymbolTable& symTab = tu.getSymbolTable();
//TODO: not finished, RamLatticeAssociation need to change, RamDomain is not suitable
class LatticeBinarytranslator {
AstTranslator& translator;
AstTranslator* translator;
const SymbolTable& symTab;
const AstLatticeBinaryFunction* AstBinary;
RamLatticeAssociation* RamLat;
int FunctionUse = 0; // 1-leq, 2-lub, 3-glb
public:
LatticeBinarytranslator(AstTranslator& translator,
LatticeBinarytranslator(AstTranslator* translator,
const SymbolTable& st, const AstLatticeBinaryFunction* AstB,
RamLatticeAssociation* RamL, int Use) :
translator(translator), symTab(st), AstBinary(AstB), RamLat(
Expand All @@ -517,37 +517,37 @@ std::unique_ptr<RamLatticeAssociation> AstTranslator::translateLatticeAssoc(
std::unique_ptr<RamValue> first = nullptr, second = nullptr,
output = nullptr;

pair.first;

// how to link to the first or second value ?
// create another valueIndex? or use the existed valueIndex ?
translator.translateValue(pair.first, valueIndex);
std::vector<std::unique_ptr<RamValue>> values;
for (AstArgument* arg : head->getArguments()) {
values.push_back(
translator.translateValue(arg, valueIndex));
}

if (auto var = dynamic_cast<AstVariable*>(arg)) {
intermediateClause->addToBody(
std::make_unique<AstBinaryConstraint>(
BinaryConstraintOp::EQ,
std::unique_ptr<AstArgument>(var->clone()),
std::make_unique<AstSubroutineArgument>(
i)));
}

RamLat->addLeq(first, second, output);

if (symTab.exist(pair.first)) {
first = new RamDomain(symTab.lookupExisting(pair.first));
}
if (symTab.exist(pair.second)) {
second = new RamDomain(symTab.lookupExisting(pair.second));
}
if (symTab.exist(pair.output)) {
output = new RamDomain(symTab.lookupExisting(pair.output));
}
// pair.first;
//
// // how to link to the first or second value ?
// // create another valueIndex? or use the existed valueIndex ?
// translator.translateValue(pair.first, valueIndex);
// std::vector<std::unique_ptr<RamValue>> values;
// for (AstArgument* arg : head->getArguments()) {
// values.push_back(
// translator.translateValue(arg, valueIndex));
// }
//
// if (auto var = dynamic_cast<AstVariable*>(arg)) {
// intermediateClause->addToBody(
// std::make_unique<AstBinaryConstraint>(
// BinaryConstraintOp::EQ,
// std::unique_ptr<AstArgument>(var->clone()),
// std::make_unique<AstSubroutineArgument>(
// i)));
// }
//
// RamLat->addLeq(first, second, output);
//
// if (symTab.exist(pair.first)) {
// first = new RamDomain(symTab.lookupExisting(pair.first));
// }
// if (symTab.exist(pair.second)) {
// second = new RamDomain(symTab.lookupExisting(pair.second));
// }
// if (symTab.exist(pair.output)) {
// output = new RamDomain(symTab.lookupExisting(pair.output));
// }

/*if (dynamic_cast<const AstConstant*>(pair.first) != nullptr) {
const auto& first_str = static_cast<const AstConstant&>(*pair.first);
Expand All @@ -565,13 +565,13 @@ std::unique_ptr<RamLatticeAssociation> AstTranslator::translateLatticeAssoc(

switch (FunctionUse) {
case 1:
RamLat->addLeq(first, second, output);
RamLat->addLeq(move(first), move(second), move(output));
break;
case 2:
RamLat->addLub(first, second, output);
RamLat->addLub(move(first), move(second), move(output));
break;
case 3:
RamLat->addGlb(first, second, output);
RamLat->addGlb(move(first), move(second), move(output));
break;
default:
assert(0 && "Not supported lattice function!");
Expand Down
9 changes: 5 additions & 4 deletions src/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,19 @@ RamDomain Interpreter::evalVal(const RamValue& value, const InterpreterContext&

RamDomain visitLatticeGLB(const RamLatticeGLB& latGLB) override {
//std::cout << "visit RamLatticeGLB here! ";
auto refs = latGLB.getRefs();
auto it = refs.begin();
const auto* refs = latGLB.getRefs();
auto it = refs->begin();
RamDomain res = ctxt[it->identifier][it->element];
//std::cout << "it->identifier:" << it->identifier << ",it->element:" << it->element << "\n";
//std::cout << "res: " << res << "\n";
it ++;
//std::cout << "refs size:" << refs.size() << "\n";
while (it != refs.end()) {
while (it != refs->end()) {
//std::cout << "it->identifier:" << it->identifier << ",it->element:" << it->element << "\n";
RamDomain it_r = ctxt[it->identifier][it->element];
//std::cout << "last_res: " << res <<" ,it_r: " << it_r << "\n";
res = interpreter.getTranslationUnit().getProgram()->getLattice()->applyGlb(res, it_r);
// TODO
// res = interpreter.getTranslationUnit().getProgram()->getLattice()->applyGlb(res, it_r);
it ++;
}
//std::cout << "visit RamLatticeGLB finish, res:" << res << "\n";
Expand Down
3 changes: 2 additions & 1 deletion src/InterpreterRelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class InterpreterRelation {
for (; it != range_end; ++it) {
const RamDomain* data = *(it);
auto curlat = data[arity-1];
biggestLat = latAssoc->applyLub(biggestLat, curlat);
// TODO
// biggestLat = latAssoc->applyLub(biggestLat, curlat);
}
high[arity-1] = biggestLat;

Expand Down
11 changes: 2 additions & 9 deletions src/RamLatticeAssociation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include "RamNode.h"
#include "RamValue.h"
#include "RamTypes.h"
#include <string>

Expand Down Expand Up @@ -242,15 +243,7 @@ class RamLatticeAssociation: public RamNode {
/** Create clone */
RamLatticeAssociation* clone() const override {
RamLatticeAssociation* res = new RamLatticeAssociation();
for (auto& cur : leq) {
res->addLeq(cur.first, cur.second, cur.output);
}
for (auto& cur : lub) {
res->addLub(cur.first, cur.second, cur.output);
}
for (auto& cur : glb) {
res->addGlb(cur.first, cur.second, cur.output);
}
//TODO
return res;
}

Expand Down
6 changes: 3 additions & 3 deletions src/RamValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "RamNode.h"
#include "RamRelation.h"
#include "SymbolTable.h"
#include "RamLatticeAssociation.h"
//#include "RamLatticeAssociation.h"

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -365,8 +365,8 @@ class RamLatticeGLB : public RamValue {
references.push_back({identifier, element, std::move(relation)});
}

std::vector<Ref_st> getRefs() const {
return references;
const std::vector<Ref_st>* getRefs() const {
return &references;
}

// const RamLatticeAssociation* getLatticeAssociation() const {
Expand Down
21 changes: 11 additions & 10 deletions src/dataflowTest/lattice_Const/const_prop.dl
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
case Bot
}

.def leq(Constant, Constant): number {
case (Bot, x) => x,
//TODO
.def leq(x: Constant, y: Constant): number {
case (Bot, _) => x,
case _ => Top
}

//TODO
.def lub(Constant, Constant): Constant {
case (Bot, x) => x,
case (x, Bot) => x,
case (x, y) => x=y ? x : Top
.def lub(x: Constant, y: Constant): Constant {
case (Bot, _) => x,
case (_, Bot) => x,
case (_, _) => x=y ? x : Top
}

.def glb(Constant, Constant): Constant {
case (Top, x) => x,
case (x, Top) => x,
case (x, y) => x=y ? x : Bot
.def glb(x: Constant, y: Constant): Constant {
case (Top, _) => x,
case (_, Top) => x,
case (_, _) => x=y ? x : Bot
}

.let Sign<> = (Bot, Top, leq, lub, glb)
Expand Down
Loading

0 comments on commit 4b03ac2

Please sign in to comment.