Skip to content

Commit

Permalink
adding directionality to parameters. (#12)
Browse files Browse the repository at this point in the history
* adding directionality to parameters.

This can currently be import/export but I expect this will
change
* adding function to check for pointer
* rename variable and change default to import
* adding better functions to indicate pointers, referecnes, and primitives
* updates to x86_64 parser

Signed-off-by: vsoch <vsoch@users.noreply.github.com>
Co-authored-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch and vsoch committed Jun 11, 2021
1 parent 3531132 commit 0b97dff
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM ghcr.io/autamus/dyninst
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y wget g++ libssl-dev libtbb-dev python3-dev build-essential \
python3-jinja2 python3-pygments ghostscript doxygen python3-yaml python3-pip clang-format
python3-jinja2 python3-pygments ghostscript doxygen python3-yaml python3-pip clang-format git
RUN cd /tmp && \
wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz && \
tar -xzvf cmake-3.20.2.tar.gz && \
Expand Down
2 changes: 1 addition & 1 deletion include/smeagle/parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace smeagle {
struct parameter {
std::string name;
std::string type;
std::string exportOrImport;
std::string direction;
std::string location;
};

Expand Down
7 changes: 4 additions & 3 deletions source/corpora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void Corpus::toAsp() {

for (auto &p : params) {
std::cout << "abi_typelocation(" << library << ", " << p.name << ", " << p.type << ", \""
<< p.location << "\")" << std::endl;
<< p.location << "\", " << p.direction << ")" << std::endl;
}
}

Expand All @@ -34,7 +34,7 @@ void Corpus::toYaml() {

for (auto &p : params) {
std::cout << " - library: " << library << "\n name: " << p.name << "\n type: " << p.type
<< "\n location: " << p.location << "\n"
<< "\n location: " << p.location << "\n direction: " << p.direction << "\n"
<< std::endl;
}
}
Expand All @@ -52,7 +52,8 @@ void Corpus::toJson() {
}

std::cout << "{\"library\": \"" << library << "\", \"name\": \"" << p.name << "\", \"type\": \""
<< p.type << "\", \"location\": \"" << p.location << "\"}" << endcomma << std::endl;
<< p.type << "\", \"location\": \"" << p.location << "\", \"direction\": \""
<< p.direction << "\"}" << endcomma << std::endl;
}
std::cout << "]}" << std::endl;
}
Expand Down
68 changes: 66 additions & 2 deletions source/detail/parser/x86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "Function.h"
#include "Symtab.h"
#include "Type.h"
#include "smeagle/parameter.h"

namespace smeagle::x86_64 {
Expand Down Expand Up @@ -76,6 +77,33 @@ namespace smeagle::x86_64 {
return sname;
}

// Return true if the dataClass is a pointer type
static bool is_pointer(st::dataClass dc) { return dc == st::dataPointer; }

// Return true if the dataClass is a reference
static bool is_ref(st::dataClass dc) { return dc == st::dataReference; }

// Return true if dataClass is reference or pointer
static bool is_indirect(st::dataClass dc) {
return dc == st::dataPointer || dc == st::dataReference;
}

// Return true if dataClass is a primitive type
static bool is_primitive(st::dataClass dc) { return dc == st::dataEnum || dc == st::dataScalar; }

// Dereference a pointer or reference
static st::Type *deref(st::Type *t) {
if (is_pointer(t->getDataClass())) {
return deref(t->getPointerType()->getConstituentType());
}
if (is_ref(t->getDataClass())) {
return deref(t->getRefType()->getConstituentType());
}
return t;
}

static bool is_typedef(st::dataClass dc) { return dc == st::dataTypedef; }

// Get a framebase for a variable based on stack location and type
int updateFramebaseFromType(st::Type *paramType, int framebase) {
// sizeof 16 with alignment bytes 16
Expand Down Expand Up @@ -128,6 +156,40 @@ namespace smeagle::x86_64 {
return result.str();
}

// Get directionality from argument type
std::string getDirectionalityFromType(st::Type *paramType) {
const auto remove_typedef = [](st::Type *t) {
if (is_typedef(t->getDataClass())) {
t = t->getTypedefType()->getConstituentType();
}
return t;
};

// Remove any top-level typedef
paramType = remove_typedef(paramType);
auto dataClass = paramType->getDataClass();

// Any type passed by value is imported
if (!is_indirect(dataClass)) {
return "import";
}

// Remove any reference or pointer indirection
paramType = deref(paramType);

// Remove any remaining typedef
paramType = remove_typedef(paramType);
dataClass = paramType->getDataClass();

// A pointer/reference to a primitive is imported
if (is_primitive(dataClass)) {
return "import";
}

// Passed by pointer or reference and not primitive, value is unknown
return "unknown";
}

// Get register class given the argument type
std::vector<RegisterClass> getRegisterClassFromType(st::Type *paramType) {
// We need a string version of the type
Expand Down Expand Up @@ -294,6 +356,9 @@ namespace smeagle::x86_64 {
// Get register class based on type
std::vector<RegisterClass> regClasses = getRegisterClassFromType(paramType);

// Get the directionality (export or import) given the type
std::string direction = getDirectionalityFromType(paramType);

// Get register name from register classes
// std::string loc = getStringLocationFromRegisterClass(regClasses);

Expand All @@ -305,8 +370,7 @@ namespace smeagle::x86_64 {
p.name = paramName;
p.type = paramType->getName();

// TODO how to determine if export/import?
p.exportOrImport = "export";
p.direction = direction;
p.location = "framebase+" + std::to_string(framebase);
typelocs.push_back(p);
order += 1;
Expand Down

0 comments on commit 0b97dff

Please sign in to comment.