Skip to content

Commit

Permalink
Add expression function evaluation, and endianness() function to retr…
Browse files Browse the repository at this point in the history
…ieve endianness of current platform
  • Loading branch information
Kingcom committed Jan 18, 2016
1 parent d3178df commit 4754ed0
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions Archs/Architecture.cpp
Expand Up @@ -14,6 +14,7 @@ ArchitectureCommand::ArchitectureCommand(const std::wstring& tempText, const std
bool ArchitectureCommand::Validate()
{
position = g_fileManager->getVirtualAddress();
g_fileManager->setEndianness(endianness);
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -34,6 +34,8 @@ add_executable(armips
Core/Common.h
Core/Expression.cpp
Core/Expression.h
Core/ExpressionFunctions.cpp
Core/ExpressionFunctions.h
Core/FileManager.cpp
Core/FileManager.h
Core/Misc.cpp
Expand Down
47 changes: 47 additions & 0 deletions Core/Expression.cpp
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "Expression.h"
#include "Common.h"
#include "ExpressionFunctions.h"

enum class ExpressionValueCombination
{
Expand Down Expand Up @@ -513,6 +514,50 @@ void ExpressionInternal::replaceMemoryPos(const std::wstring& identifierName)
}
}

ExpressionValue ExpressionInternal::executeFunctionCall()
{
ExpressionValue invalid;

// find function, check parameter counts
auto it = expressionFunctions.find(strValue);
if (it == expressionFunctions.end())
{
Logger::queueError(Logger::Error,L"Unknown function \"%s\"",strValue);
return invalid;
}

if (it->second.minParams > childrenCount)
{
Logger::queueError(Logger::Error,L"Not enough parameters for \"%s\" (min %d)",strValue,it->second.minParams);
return invalid;
}

if (it->second.maxParams < childrenCount)
{
Logger::queueError(Logger::Error,L"Too many parameters for \"%s\" (min %d)",strValue,it->second.maxParams);
return invalid;
}

// evaluate parameters
std::vector<ExpressionValue> params;
params.reserve(childrenCount);

for (size_t i = 0; i < childrenCount; i++)
{
ExpressionValue result = children[i]->evaluate();
if (result.isValid() == false)
{
Logger::queueError(Logger::Error,L"Invalid expression");
return result;
}

params.push_back(result);
}

// execute
return it->second.function(params);
}

ExpressionValue ExpressionInternal::evaluate()
{
ExpressionValue val;
Expand Down Expand Up @@ -620,6 +665,8 @@ ExpressionValue ExpressionInternal::evaluate()
return children[2]->evaluate();
else
return children[1]->evaluate();
case OperatorType::FunctionCall:
return executeFunctionCall();
default:
return val;
}
Expand Down
1 change: 1 addition & 0 deletions Core/Expression.h
Expand Up @@ -128,6 +128,7 @@ class ExpressionInternal
void allocate(size_t count);
void deallocate();
std::wstring formatFunctionCall();
ExpressionValue executeFunctionCall();

OperatorType type;
ExpressionInternal** children;
Expand Down
28 changes: 28 additions & 0 deletions Core/ExpressionFunctions.cpp
@@ -0,0 +1,28 @@
#include "stdafx.h"
#include "ExpressionFunctions.h"
#include "Misc.h"
#include "Common.h"

ExpressionValue expressionFunctionEndianness(const std::vector<ExpressionValue>& parameters)
{
Endianness endianness = g_fileManager->getEndianness();

ExpressionValue result;
result.type = ExpressionValueType::String;

switch (endianness)
{
case Endianness::Little:
result.strValue = L"little";
break;
case Endianness::Big:
result.strValue = L"big";
break;
}

return result;
}

const ExpressionFunctionMap expressionFunctions = {
{ L"endianness", { &expressionFunctionEndianness, 0, 0 } },
};
15 changes: 15 additions & 0 deletions Core/ExpressionFunctions.h
@@ -0,0 +1,15 @@
#pragma once
#include "Expression.h"
#include <map>

typedef ExpressionValue (*ExpressionFunction)(const std::vector<ExpressionValue>&);

struct ExpressionFunctionEntry {
ExpressionFunction function;
size_t minParams;
size_t maxParams;
};

typedef std::map<std::wstring, const ExpressionFunctionEntry> ExpressionFunctionMap;

extern const ExpressionFunctionMap expressionFunctions;
1 change: 1 addition & 0 deletions Core/FileManager.h
Expand Up @@ -77,6 +77,7 @@ class FileManager
bool advanceMemory(size_t bytes);
AssemblerFile* getOpenFile() { return activeFile; };
void setEndianness(Endianness endianness) { this->endianness = endianness; };
Endianness getEndianness() { return endianness; }
private:
bool checkActiveFile();
std::vector<AssemblerFile*> files;
Expand Down
2 changes: 2 additions & 0 deletions libarmips.vcxproj
Expand Up @@ -244,6 +244,7 @@
<ClCompile Include="Core\ELF\ElfFile.cpp" />
<ClCompile Include="Core\ELF\ElfRelocator.cpp" />
<ClCompile Include="Core\Expression.cpp" />
<ClCompile Include="Core\ExpressionFunctions.cpp" />
<ClCompile Include="Core\FileManager.cpp" />
<ClCompile Include="Core\Misc.cpp" />
<ClCompile Include="Core\SymbolData.cpp" />
Expand Down Expand Up @@ -297,6 +298,7 @@
<ClInclude Include="Core\ELF\ElfRelocator.h" />
<ClInclude Include="Core\ELF\ElfTypes.h" />
<ClInclude Include="Core\Expression.h" />
<ClInclude Include="Core\ExpressionFunctions.h" />
<ClInclude Include="Core\FileManager.h" />
<ClInclude Include="Core\Misc.h" />
<ClInclude Include="Core\SymbolData.h" />
Expand Down
6 changes: 6 additions & 0 deletions libarmips.vcxproj.filters
Expand Up @@ -160,6 +160,9 @@
<ClCompile Include="Archs\MIPS\MipsParser.cpp">
<Filter>Archs\MIPS</Filter>
</ClCompile>
<ClCompile Include="Core\ExpressionFunctions.cpp">
<Filter>Core</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Archs\Architecture.h">
Expand Down Expand Up @@ -295,5 +298,8 @@
<ClInclude Include="Archs\MIPS\MipsParser.h">
<Filter>Archs\MIPS</Filter>
</ClInclude>
<ClInclude Include="Core\ExpressionFunctions.h">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 4754ed0

Please sign in to comment.