Page Menu
Home
Xreate
Search
Configure Global Search
Log In
Docs
Questions
Repository
Issues
Patches
Internal API
Files
F5333756
dfgpass.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Nov 21, 4:22 AM
Size
5 KB
Mime Type
text/x-c++
Expires
Mon, Nov 23, 4:22 AM (1 d, 9 h)
Engine
blob
Format
Raw Data
Handle
316259
Attached To
rXR Xreate
dfgpass.cpp
View Options
#include "dfgpass.h"
#include "passmanager.h"
#include "clasplayer.h"
#include <boost/format.hpp>
using namespace xreate;
using namespace std;
DFGPass::DFGPass(PassManager* manager)
: AbstractPass(manager), clasp(man->clasp)
{}
SymbolNode
DFGPass::process(ManagedFnPtr function){
SymbolNode symbolRet = AbstractPass::process(function);
if (SymbolPacked* symbolRetPacked = boost::get<SymbolPacked>(&symbolRet)){
Expression retExpr(Operator::CALL, {Atom<Identifier_t>(Config::get("clasp.ret.symbol")),
Atom<Identifier_t>(boost::str(boost::format("(%1%, %2%)")
%(symbolRetPacked->identifier)
% (symbolRetPacked->scope)))
});
const std::vector<Tag> tag{{retExpr, TagModifier::NONE}};
clasp->cfagraph.addFunctionNodeTags(function->getName(), tag);
} else if (SymbolTransient* symbT = boost::get<SymbolTransient>(&symbolRet)){
std::vector<Tag> tags;
tags.reserve(symbT->tags.size());
const std::string stmntRetTag = Config::get("clasp.ret.tag");
std::transform(symbT->tags.begin(), symbT->tags.end(), std::inserter(tags, tags.begin()),
[&stmntRetTag](const Expression& e) {
Expression tag(Operator::CALL, {Atom<Identifier_t>(string(stmntRetTag)), e});
return Tag{e, TagModifier::NONE};
});
clasp->cfagraph.addFunctionNodeTags(function->getName(), tags);
}
}
SymbolNode
DFGPass::process(const Expression& expression, PassContext context, const std::string& decl)
{
// write down adhoc expression tags:
if (expression.tags.size() && decl.length()) {
for (pair<std::string, Expression> tag: expression.tags) {
SymbolNode nodeThis = clasp->pack(context.scope->findSymbol(decl), decl);
__context.graph.addTag(nodeThis, Expression(tag.second));
}
}
switch(expression.__state) {
case Expression::IDENT:
const string& ident = expression.getValueString();
SymbolNode nodeFrom = AbstractPass::process(expression, context, decl);
SymbolPacked nodeTo = clasp->pack(context.scope->findSymbol(ident));
__context.graph.addLink(nodeTo, nodeFrom, DFGConnection::ALIAS);
return nodeTo;
}
//special case for NONE value:
if (expression.isNone()){
return SymbolTransient{{Atom<Identifier_t>(Config::get("clasp.nonevalue"))}};
}
switch(expression.op) {
case Operator::CALL: {
const string &name = expression.getValueString();
std::vector<SymbolNode> operands;
operands.reserve(expression.getOperands().size());
for (const Expression &op: expression.getOperands()) {
operands.push_back(process(op, context));
}
ManagedFnPtr function = man->root->findFunction(name);
if (!function) return SymbolInvalid();
// set calling relations:
CodeScope *scopeRemote = function->getEntryScope();
std::vector<SymbolNode>::iterator op = operands.begin();
for (const std::string &arg: scopeRemote->__args) {
const Symbol &nodeRemote = scopeRemote->findSymbol(arg);
SymbolPacked* operandPacked = boost::get<SymbolPacked>(&*op);
assert(operandPacked);
__context.graph.addLink(clasp->pack(nodeRemote), *operandPacked, DFGConnection::OPT);
++op;
}
//TODO need SymbolTransient::connection mark in order to represent OPT connection
return clasp->pack(Symbol{0, scopeRemote});
}
}
std::vector<SymbolNode> operands;
if (expression.__state != Expression::COMPOUND){
AbstractPass::process(expression, context, decl);
return SymbolInvalid();
}
if (__signatures.count(expression.op)) {
const Expression &scheme = __signatures.at(expression.op);
operands.reserve(expression.getOperands().size());
if (expression.op == Operator::CALL || expression.op == Operator::INDEX){
string caption = expression.getValueString();
operands.push_back(process(Expression(move(caption)), context, ""));
}
for (const Expression &op: expression.getOperands()) {
operands.push_back(process(op, context));
}
std::vector<SymbolNode>::iterator arg = operands.begin();
std::vector<Expression>::const_iterator tag = ++scheme.getOperands().begin();
while (tag != scheme.getOperands().end()) {
if (tag->__state != Expression::INVALID) {
__context.graph.addTag(*arg, Expression(*tag));
}
++arg; ++tag;
}
Expression retTag = *scheme.getOperands().begin();
if (retTag.__state != Expression::INVALID) {
return SymbolTransient{{retTag}};
}
}
// adhoc for MAP case, TODO reorganize code in more clear manner
if (expression.op == Operator::MAP) {
SymbolNode nodeFrom;
if (operands.size()) {
nodeFrom = operands.at(0);
} else {
nodeFrom = process(expression.getOperands().at(0), context);
}
assert(!decl.empty());
SymbolPacked nodeTo = clasp->pack(context.scope->findSymbol(decl));
SymbolPacked* nodeFromPacked = boost::get<SymbolPacked>(&nodeFrom);
assert(nodeFromPacked);
__context.graph.addLink(move(nodeTo), *nodeFromPacked, DFGConnection::PROTO);
}
if (__signatures.count(expression.op) || expression.op == Operator::MAP) {
for (CodeScope* scope: expression.blocks) {
AbstractPass::process(scope, context);
}
return SymbolInvalid();
}
return SymbolInvalid();
}
void
DFGPass::run()
{
init();
return AbstractPass::run();
}
void
DFGPass::init()
{
for (const Expression& scheme: man->root->__dfadata)
{
__signatures.emplace(scheme.op, scheme);
}
}
void DFGPass::finish()
{
man->clasp->addDFAData(move(__context.graph));
}
Event Timeline
Log In to Comment