Skip to content

Commit

Permalink
closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
partouf committed Jan 7, 2022
1 parent 549869e commit cc5a031
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 17 deletions.
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

sudo -H apt install -y python3 python3-pip python3-setuptools g++-9
sudo -H apt install -y python3 python3-pip python3-setuptools g++-9 patchelf
sudo -H pip3 install conan

conan profile new default --detect
Expand Down
14 changes: 10 additions & 4 deletions src/assembly/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ void AsmParser::AssemblyTextParser::extractUsedLabelsFromDataLine(const std::str
for (auto &label_ref : this->state.currentLine->labels)
{
this->data_used_labels[label_ref.name].insert(this->state.previousLabel);

if (this->state.currentSourceRef.inside_proc)
{
this->data_used_labels[label_ref.name].insert(this->state.previousParentLabel);
}
}
}

Expand Down Expand Up @@ -352,7 +357,7 @@ void AsmParser::AssemblyTextParser::eol()
this->state.currentLine->text = std::move(filteredLine);
}

if (AssemblyTextParserUtils::startBlock(this->state.currentLine->text) && this->state.currentSourceRef.line == 0)
if (AssemblyTextParserUtils::startProcBlock(this->state.currentLine->text) && this->state.currentSourceRef.line == 0)
{
this->markPreviousInternalLabelAsInsideProc();
if (this->filter.directives)
Expand Down Expand Up @@ -586,7 +591,7 @@ bool AsmParser::AssemblyTextParser::isUsedThroughAlias(const std::string_view la

bool AsmParser::AssemblyTextParser::isUsed(const std::string_view label, const int depth) const
{
if (usercode_labels.contains(label))
if (this->usercode_labels.contains(label))
return true;

const auto usedfind = this->used_labels.find(label);
Expand Down Expand Up @@ -742,7 +747,7 @@ void AsmParser::AssemblyTextParser::removeUnused()

if (remove || removeOnlyThis ||
(!isUsed && !isUsedThroughAlias && !isDataUsedThroughAlias && this->filter.compatmode &&
this->filter.directives && line->is_data))
this->filter.directives && line->is_data && !line->source.inside_proc))
{
// filter this out
this->state.filteredlines.push_back(std::move(line));
Expand Down Expand Up @@ -835,7 +840,8 @@ void AsmParser::AssemblyTextParser::outputDebugJson(std::ostream &out) const
{
const std::vector<asm_labelpair> labels = this->redetermineLabels();

DebugJsonWriter writer(out, this->lines, labels, this->filter, this->used_labels, this->weakly_used_labels, this->aliased_labels);
DebugJsonWriter writer(out, this->lines, labels, this->filter, this->used_labels, this->weakly_used_labels,
this->aliased_labels, this->data_used_labels);
writer.write();
}

Expand Down
7 changes: 5 additions & 2 deletions src/test/test_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ TEST_CASE("Test text assembly utilities", "[asm]")
REQUIRE(file_def.value().file_index == 2);
REQUIRE(file_def.value().file_name == "/opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/bits/char_traits.h");

const auto endproc = AsmParser::AssemblyTextParserUtils::endBlock("\t.cfi_endproc");
CHECK(endproc);
const auto endproc1 = AsmParser::AssemblyTextParserUtils::endBlock("\t.cfi_endproc");
CHECK(endproc1);

const auto endproc2 = AsmParser::AssemblyTextParserUtils::endProcBlock("\t.cfi_endproc");
CHECK(endproc2);

const auto indentedLabel = AsmParser::AssemblyTextParserUtils::getLineWithoutComment("\tlabel:");
REQUIRE(indentedLabel == "\tlabel:");
Expand Down
39 changes: 37 additions & 2 deletions src/utils/jsonwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,10 @@ AsmParser::DebugJsonWriter::DebugJsonWriter(std::ostream &out,
const Filter filter,
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_labels,
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_weak_labels,
const std::unordered_map<std::string_view, std::string_view> aliased_labels)
const std::unordered_map<std::string_view, std::string_view> aliased_labels,
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_data_labels)
: AsmParser::JsonWriter::JsonWriter(out, lines, labels, filter), used_labels(used_labels),
used_weak_labels(used_weak_labels), aliased_labels(aliased_labels)
used_weak_labels(used_weak_labels), aliased_labels(aliased_labels), used_data_labels(used_data_labels)
{
}

Expand Down Expand Up @@ -560,6 +561,40 @@ void AsmParser::DebugJsonWriter::write()
}
this->out << "}";

this->out << ",\n";
this->writeKeyName("used_data_labels");
this->out << "{";
firstLabel = true;
for (auto &usedlabel : this->used_data_labels)
{
if (firstLabel)
{
firstLabel = false;
}
else
{
this->out << ",";
}

this->writeKeyName(usedlabel.first);
this->out << "[";
bool firstref = true;
for (auto &ref : usedlabel.second)
{
if (firstref)
{
firstref = false;
this->writeValue(ref, jsonopt::none);
}
else
{
this->writeValue(ref, jsonopt::prefixwithcomma);
}
}
this->out << "]";
}
this->out << "}";

this->out << ",\n";
this->writeKeyName("aliased_labels");
this->out << "{";
Expand Down
4 changes: 3 additions & 1 deletion src/utils/jsonwriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class DebugJsonWriter : public JsonWriter
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_labels;
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_weak_labels;
const std::unordered_map<std::string_view, std::string_view> aliased_labels;
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_data_labels;

void writeDebugLine(const asm_line_v *line);

Expand All @@ -68,7 +69,8 @@ class DebugJsonWriter : public JsonWriter
const Filter filter,
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_labels,
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_weak_labels,
const std::unordered_map<std::string_view, std::string_view> aliased_labels);
const std::unordered_map<std::string_view, std::string_view> aliased_labels,
const std::unordered_map<std::string_view, std::unordered_set<std::string_view>> used_data_labels);

void write() override;
};
Expand Down
3 changes: 2 additions & 1 deletion src/utils/regexes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ struct Regexes
static constexpr auto sourceD2Tag = ctre::match<R"re(^\h*\.d2line\h+(\d+),?\h*(\d*).*)re">;
static constexpr auto sourceD2File = ctre::match<R"re(^\h*\.d2file\h+"(.*)")re">;
static constexpr auto stdInLooking = ctre::search<R"re(<stdin>|^-$|example\.[^/]+$|<source>)re">;
static constexpr auto startBlock = ctre::search<R"re(\.cfi_startproc)re">;
static constexpr auto startProcBlock = ctre::search<R"re(\.cfi_startproc)re">;
static constexpr auto endBlock = ctre::search<R"re(\.(cfi_endproc|data|text|section))re">;
static constexpr auto endProcBlock = ctre::search<R"re(\.cfi_endproc)re">;

static constexpr auto sectionDef = ctre::search<R"re(\.(data|text|section)\h*"?([.a-zA-Z0-9\-]*)"?)re">;

Expand Down
12 changes: 10 additions & 2 deletions src/utils/regexwrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,9 @@ bool AsmParser::AssemblyTextParserUtils::endAsmNesting(const std::string_view li
return false;
}

bool AsmParser::AssemblyTextParserUtils::startBlock(const std::string_view line)
bool AsmParser::AssemblyTextParserUtils::startProcBlock(const std::string_view line)
{
if (Regexes::startBlock(line))
if (Regexes::startProcBlock(line))
return true;

return false;
Expand All @@ -428,6 +428,14 @@ bool AsmParser::AssemblyTextParserUtils::endBlock(const std::string_view line)
return false;
}

bool AsmParser::AssemblyTextParserUtils::endProcBlock(const std::string_view line)
{
if (Regexes::endProcBlock(line))
return true;

return false;
}

std::optional<std::string_view> AsmParser::AssemblyTextParserUtils::getLabel(const std::string_view line)
{
auto match = Regexes::labelDef(line);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/regexwrappers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class AssemblyTextParserUtils
static bool startAsmNesting(const std::string_view line);
static bool endAsmNesting(const std::string_view line);

static bool startBlock(const std::string_view line);
static bool startProcBlock(const std::string_view line);
static bool endBlock(const std::string_view line);
static bool endProcBlock(const std::string_view line);

static bool isCudaEndDef(const std::string_view line);
static bool isDataDefn(const std::string_view line);
Expand Down
9 changes: 6 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PATH=$PATH:/opt/compiler-explorer/cmake/bin
export CXX=/opt/compiler-explorer/gcc-11.2.0/bin/g++
export CC=/opt/compiler-explorer/gcc-11.2.0/bin/gcc
export CXXFLAGS="-I$PWD/ctre/include"
export LD_LIBRARY_PATH=/opt/compiler-explorer/gcc-11.2.0/lib64
# export LD_LIBRARY_PATH=/opt/compiler-explorer/gcc-11.2.0/lib64

# export CXX=/opt/compiler-explorer/clang-12.0.0/bin/clang++
# export CC=/opt/compiler-explorer/clang-12.0.0/bin/clang
Expand Down Expand Up @@ -43,6 +43,8 @@ if [ $? -ne 0 ]; then
exit $?
fi

patchelf --set-rpath /opt/compiler-explorer/gcc-11.2.0/lib64 bin/test

bin/test
if [ $? -ne 0 ]; then
exit $?
Expand All @@ -54,10 +56,11 @@ if [ $? -ne 0 ]; then
exit $?
fi

patchelf --set-rpath /opt/compiler-explorer/gcc-11.2.0/lib64 bin/asm-parser

cd ..

echo 995test
/usr/bin/time --verbose build/bin/asm-parser -comment_only -directives -unused_labels /opt/compiler-explorer/ce/test/filters-cases/bug-995.asm > bla.json
# /usr/bin/time --verbose build/bin/asm-parser -comment_only -directives -unused_labels /opt/compiler-explorer/ce/test/filters-cases/bug-995.asm > bla.json

# echo bintest-1
# build/bin/asm-parser -binary /opt/compiler-explorer/ce/test/filters-cases/bintest-1.asm > /opt/compiler-explorer/ce/test/filters-cases/bintest-1.asm.binary.directives.labels.comments.json
Expand Down

0 comments on commit cc5a031

Please sign in to comment.