Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cccppp/include/builtins.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bool __builtin_is_constant_evaluated();
int __builtin_va_arg_pack();
2 changes: 1 addition & 1 deletion cccppp/include/prelude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct __has_subscript_overload
template <class U>
static auto subscript_test(const U* u) -> decltype(operator[]((*u), 0), char(0)) {}
// a static member function that returns a length-2 thing
static short subscript_test(...) {}
static short subscript_test(...) {return 0;}
static const bool value = (sizeof(subscript_test((T*)0)) == 1);
};

Expand Down
5 changes: 3 additions & 2 deletions cccppp/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LLVM_CONFIG ?= llvm-config-13
# possibly be relevant... the linker will figure otu what to pull in.
# This changes by version of LLVM. They are all in a group, so
# the order doesn't matter (it's alphabetical).
dump cccppp-tool interstitial-tool simple-tool: LDLIBS += \
dump cccppp-tool interstitial-tool simple-tool easyTool-tool: LDLIBS += \
-Wl,--start-group \
-lclangAST \
-lclangASTMatchers \
Expand Down Expand Up @@ -52,7 +52,8 @@ interstitial-tool: interstitial.o
$(CXX) -o $@ $+ $(LDFLAGS) $(LDLIBS)
dump: dump.o
$(CXX) -o $@ $+ $(LDFLAGS) $(LDLIBS)

simple-tool: simple.o
$(CXX) -o $@ $+ $(LDFLAGS) $(LDLIBS)
OCAMLOPTFLAGS += -fPIC
CFLAGS += -fPIC

Expand Down
365 changes: 365 additions & 0 deletions cccppp/src/Working-Interstitial

Large diffs are not rendered by default.

125 changes: 101 additions & 24 deletions cccppp/src/interstitial.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <sstream>
#include <string>
#include <iostream>
#ifdef USE_STD_UNIQUE_PTR
#include <memory>
#endif
Expand Down Expand Up @@ -39,6 +40,9 @@ using llvm::make_unique;
#endif
using llvm::Optional;

std::set<unsigned int> statementOffsets;
std::vector<std::pair<unsigned int, unsigned int>> rewrittenRanges;

class SimpleRewriteASTVisitor : public RecursiveASTVisitor<SimpleRewriteASTVisitor> {
public:
bool shouldTraversePostOrder() const /* override */ { return true; }
Expand Down Expand Up @@ -195,45 +199,83 @@ Initially right interstice is: `0]' should be ']'
// forwards and its end backwards.
// the start of eSubLeft and the start of eSubRight
// It's easiest to do this right-to-left, and calculating lengths first.
unsigned leftIntersticeLength = rangeLength(mkRightOpenRange(eOuter->getSourceRange().getBegin(),
//
// Is this better name for variables ?
/*
unsigned leftIntersticeEstimatedLength = rangeLength(mkRightOpenRange(eOuter->getSourceRange().getBegin(),
eSubLeft->getSourceRange().getBegin(), false, false));
unsigned beforeMidIntersticeEstimatedLength = rangeLength(mkRightOpenRange(eSubLeft->getSourceRange().getEnd(),
eSubRight->getSourceRange().getBegin(), true, false));
unsigned rightIntersticeEstimatedLength = rangeLength(mkRightOpenRange(eSubRight->getSourceRange().getEnd(),
eOuter->getSourceRange().getEnd(), true, true));
*/

// or something like this: beforelhSubExprLength, lhSubExprLength, afterRhExprLength


unsigned leftIntersticeLength = rangeLength(mkRightOpenRange(eOuter->getSourceRange().getBegin(),
eSubLeft->getSourceRange().getBegin(), false, false));
unsigned midIntersticeLength = rangeLength(mkRightOpenRange(eSubLeft->getSourceRange().getEnd(),
eSubRight->getSourceRange().getBegin(), true, false));
unsigned rightIntersticeLength = rangeLength(mkRightOpenRange(eSubRight->getSourceRange().getEnd(),
eOuter->getSourceRange().getEnd(), true, true));
/* Now we've got the lengths, get the start positions. */
eOuter->getSourceRange().getEnd(), true, true));

/* Now we've got the lengths, get the start positions. */

llvm::errs() << "Length of leftInterstice is (according to range) :-> " << leftIntersticeLength << "\n";
llvm::errs() << "Length of midInterstice is (according to range) :-> " << midIntersticeLength << "\n";
llvm::errs() << "Length of rightInterstice is (according to range) :-> " << rightIntersticeLength << "\n";

SourceLocation leftIntersticeStart = eOuter->getSourceRange().getBegin();
SourceLocation midIntersticeStart = eSubLeft->getSourceRange().getEnd().getLocWithOffset(1);
SourceLocation rightIntersticeStart = eSubRight->getSourceRange().getEnd().getLocWithOffset(1);
SourceLocation leftIntersticeEnd = eSubLeft->getSourceRange().getBegin().getLocWithOffset(-1);
std::pair<FileID, unsigned> leftStartDecomposedLoc = TheRewriter.getSourceMgr().getDecomposedLoc(leftIntersticeStart);
llvm::errs() << "DecomposedLoc for LeftStartInterstice :-> " << leftStartDecomposedLoc.second << "\n";

SourceLocation midIntersticeStart = eSubLeft->getSourceRange().getEnd().getLocWithOffset(midIntersticeLength);
std::pair<FileID, unsigned> midStartDecomposedLoc = TheRewriter.getSourceMgr().getDecomposedLoc(midIntersticeStart);
llvm::errs() << "DecomposedLoc for MidStartInterstice :-> " << midStartDecomposedLoc.second << "\n";

SourceLocation rightIntersticeStart = eSubRight->getSourceRange().getEnd().getLocWithOffset(rightIntersticeLength);
std::pair<FileID, unsigned> rightStartDecomposedLoc = TheRewriter.getSourceMgr().getDecomposedLoc(rightIntersticeStart);
llvm::errs() << "DecomposedLoc for RightStartInterstice :-> " << rightStartDecomposedLoc.second << "\n";

SourceLocation leftIntersticeEnd = eSubLeft->getSourceRange().getBegin().getLocWithOffset(-1); //Initially it was set to -1
std::pair<FileID, unsigned> leftEndDecomposedLoc = TheRewriter.getSourceMgr().getDecomposedLoc(leftIntersticeEnd);
llvm::errs() << "DecomposedLoc for LeftEndInterstice :-> " << leftEndDecomposedLoc.second << "\n";

SourceLocation midIntersticeEnd = eSubRight->getSourceRange().getBegin().getLocWithOffset(-1);
SourceLocation rightIntersticeEnd = eOuter->getSourceRange().getEnd();
std::pair<FileID, unsigned> midEndDecomposedLoc = TheRewriter.getSourceMgr().getDecomposedLoc(midIntersticeEnd);
llvm::errs() << "DecomposedLoc for MidEndInterstice :-> " << midEndDecomposedLoc.second << "\n";

SourceLocation rightIntersticeEnd = eOuter->getSourceRange().getEnd();
std::pair<FileID, unsigned> rightEndDecomposedLoc = TheRewriter.getSourceMgr().getDecomposedLoc(rightIntersticeEnd);
llvm::errs() << "DecomposedLoc for RightEndInterstice :-> " << rightEndDecomposedLoc.second << "\n";
// now does this work?

Optional<SourceRange> leftIntersticeRange = mkStartLengthRange(
leftIntersticeStart, leftIntersticeLength);
Optional<SourceRange> midIntersticeRange = mkStartLengthRange(
midIntersticeStart, midIntersticeLength);
Optional<SourceRange> rightIntersticeRange = mkStartLengthRange(
rightIntersticeStart, rightIntersticeLength);

/*SourceRange(leftIntersticeStart, leftIntersticeEnd);*/leftIntersticeStart, leftIntersticeLength);
Optional<SourceRange> midIntersticeRange = //mkStartLengthRange(
SourceRange(midIntersticeStart, midIntersticeEnd);/*midIntersticeStart, midIntersticeLength)*/;
Optional<SourceRange> rightIntersticeRange = //mkStartLengthRange(
SourceRange(rightIntersticeStart, rightIntersticeEnd); /*rightIntersticeStart, rightIntersticeLength);*/
/* The sum of lengths of the interstices should be equal to the length of the whole expression
* minus the lengths of the subexpression. */
unsigned intersticesLength = rangeLength(leftIntersticeRange)
/*unsigned intersticesLength = rangeLength(leftIntersticeRange)
+ rangeLength(midIntersticeRange)
+ rangeLength(rightIntersticeRange);
+ rangeLength(rightIntersticeRange);*/
//assert(intersticesLength + leftSubLength + rightSubLength == outerLength);

llvm::errs() << "Initially left interstice (length " << rangeLength(leftIntersticeRange) << ") is: `"
<< (!leftIntersticeRange ? "" : TheRewriter.getRewrittenText(*leftIntersticeRange)) << "'\n";
<< (!leftIntersticeRange ? "" : TheRewriter.getRewrittenText(*leftIntersticeRange)) << "'\n";
llvm::errs() << "Initially mid interstice (length " << rangeLength(midIntersticeRange) << ") is: `"
<< (!midIntersticeRange ? "" : TheRewriter.getRewrittenText(*midIntersticeRange)) << "'\n";
<< (!midIntersticeRange ? "" : TheRewriter.getRewrittenText(*midIntersticeRange)) << "'\n";
llvm::errs() << "Initially right interstice (length " << rangeLength(rightIntersticeRange) << ") is: `"
<< (!rightIntersticeRange ? "" : TheRewriter.getRewrittenText(*rightIntersticeRange)) << "'\n";

/* Do three small rewrites, not one big one. */
if (leftIntersticeRange) TheRewriter.ReplaceText(
leftIntersticeRange->getBegin(),
TheRewriter.getRewrittenText(*leftIntersticeRange).length(),
TheRewriter.getRewrittenText(*leftIntersticeRange).length()/*leftIntersticeLength*/,
leftInterstice
);
else { TheRewriter.InsertTextBefore(eOuter->getSourceRange().getBegin(), leftInterstice);
Expand Down Expand Up @@ -265,7 +307,7 @@ Initially right interstice is: `0]' should be ']'
midIntersticeRange->getBegin(),
TheRewriter.getRewrittenText(*midIntersticeRange).length(),
midInterstice
); else TheRewriter.InsertTextAfter(eSubLeft->getSourceRange().getEnd(), midInterstice);
); else TheRewriter.InsertTextAfter(eSubLeft->getSourceRange().getEnd().getLocWithOffset(midIntersticeLength), midInterstice);
if (rightIntersticeRange) TheRewriter.ReplaceText(
rightIntersticeRange->getBegin(),
TheRewriter.getRewrittenText(*rightIntersticeRange).length(),
Expand Down Expand Up @@ -334,6 +376,7 @@ Initially right interstice is: `0]' should be ']'
llvm::errs() << "Skipping something under a sizeof, _Alignof, decltype or similar expr!\n";
return true;
}

return false;
}();

Expand All @@ -356,13 +399,40 @@ Initially right interstice is: `0]' should be ']'
llvm::errs() << "\n";
e->dump();
std::string lhBefore = TheRewriter.getRewrittenText(e->getLHS()->getSourceRange());
llvm::errs() << "The lhBefore :-> " << lhBefore << "\n";
std::string rhBefore = TheRewriter.getRewrittenText(e->getRHS()->getSourceRange());
// replace it with some text we have crafted
llvm::errs() << "The rhBefore :-> " << rhBefore << "\n";
QualType indexedType = e->getLHS()->getType();
// is this definitely an array?
std::string leftInterstice;
std::string midInterstice = ", ";
std::string rightInterstice = ")";

unsigned int offset = TheRewriter.getSourceMgr().getFileOffset(e->getLHS()->getBeginLoc()); // Calculate the offset
unsigned int endOffset = TheRewriter.getSourceMgr().getFileOffset(e->getRHS()->getEndLoc());

llvm::errs() << "\e[1;32m Replacing at offset: \e[0m" << "\e[1;32m Beginning offset: \e[0m" << offset << "\e[1;32m Ending offset: \e[0m" << endOffset << "\n";
// Check if the current range falls within any previously rewritten range
bool withinPreviousRange = false;
for (const auto& range : rewrittenRanges) {
if (offset == range.first && endOffset == range.second) {
withinPreviousRange = true;
break;
}
}

std::string string1= "__primop_subscript<";
std::string string2= "__maybe_primop_subscript<";

std::size_t found1 = TheRewriter.getRewrittenText(e->getLHS()->getSourceRange()).find(string1);
std::size_t found2 = TheRewriter.getRewrittenText(e->getLHS()->getSourceRange()).find(string2);

if(withinPreviousRange && (found1 != std::string::npos || found2 != std::string::npos)) {
llvm::errs() << "Already Translated" << "\n";
return true;
}

if (!(indexedType.getTypePtr()->isTemplateTypeParmType()
|| indexedType.getTypePtr()->isDependentType()
|| indexedType.getTypePtr()->isInstantiationDependentType()
Expand All @@ -380,13 +450,20 @@ Initially right interstice is: `0]' should be ']'
* as it already appears in the code. But in many cases this
* comes out as "<dependent type>", and I haven't figured out a
* way to make clang print what we want. So use decltype() for now. */
//flag = "0";
leftInterstice = std::string("__maybe_primop_subscript<")
+ "decltype("
+ lhBefore
+ "), !__has_subscript_overload<decltype(" + lhBefore + ")>::value>()(";

//This was not here before
}

ReplaceBinaryExpressionInterstices(e, e->getLHS(), e->getRHS(),
leftInterstice, midInterstice, rightInterstice);

rewrittenRanges.emplace_back(offset, endOffset);
//added this just to check
// after replacement, we should still have the same view of the subexpressions
// FIXME: EXCEPT we can't do this if the LHS begins at the same place as the
// outer expression
Expand All @@ -396,7 +473,7 @@ Initially right interstice is: `0]' should be ']'
}
return true;
}

private:
Rewriter &TheRewriter;
ASTContext &TheContext;
Expand Down Expand Up @@ -431,7 +508,7 @@ class SimpleRewriteConsumer : public ASTConsumer {
bool HandleTopLevelDecl(DeclGroupRef DR) override {
unsigned count = 0;
SourceLocation lastSourceLoc;
//llvm::errs() << "== Saw top-level decl\n";
llvm::errs() << "== Saw top-level decl\n";
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
// HACK: to get parent info, I have to do this, but I have no idea why.
C.setTraversalScope({*b});
Expand All @@ -445,8 +522,8 @@ class SimpleRewriteConsumer : public ASTConsumer {
llvm::errs() << "== The last one ended at ";
lastSourceLoc.print(llvm::errs(), R.getSourceMgr());
llvm::errs() << " (written in main file? "
<< R.getSourceMgr().isWrittenInMainFile(lastSourceLoc)
<< ", presumed in main file? "
<< R.getSourceMgr().isWrittenInMainFile(lastSourceLoc)
<< ", presumed in main file? "
<< R.getSourceMgr().isInMainFile(lastSourceLoc)
<< "; immediate spelling loc: ";
R.getSourceMgr().getImmediateSpellingLoc(lastSourceLoc).print(llvm::errs(), R.getSourceMgr());
Expand Down
Loading