Permalink
Browse files

Merge pull request #147 from Sagiri/definelabel_cmd

Adds -definelabel command line argument
  • Loading branch information...
Kingcom committed Feb 9, 2019
2 parents 7885552 + a920a7e commit d7e3ba41539d246fb4161da2c1c009a8a21f3da1
Showing with 89 additions and 59 deletions.
  1. +28 −8 Main/main.cpp
  2. +2 −51 Parser/Tokenizer.cpp
  3. +3 −0 Readme.md
  4. +55 −0 Util/Util.cpp
  5. +1 −0 Util/Util.h
@@ -3,6 +3,7 @@
#include "Core/Assembler.h"
#include "Archs/MIPS/Mips.h"
#include "Commands/CDirectiveFile.h"
#include "Util/Util.h"
#include "Tests.h"

#if defined(_WIN64) || defined(__x86_64__) || defined(__amd64__)
@@ -18,16 +19,17 @@ void printUsage(std::wstring executableName)
Logger::printLine(L"Usage: %s [optional parameters] <FILE>", executableName);
Logger::printLine(L"");
Logger::printLine(L"Optional parameters:");
Logger::printLine(L" -temp <TEMP> Output temporary assembly data to <TEMP> file");
Logger::printLine(L" -sym <SYM> Output symbol data in the sym format to <SYM> file");
Logger::printLine(L" -sym2 <SYM2> Output symbol data in the sym2 format to <SYM2> file");
Logger::printLine(L" -root <ROOT> Use <ROOT> as working directory during execution");
Logger::printLine(L" -equ <NAME> <VAL> Equivalent to \'<NAME> equ <VAL>\' in code");
Logger::printLine(L" -strequ <NAME> <VAL> Equivalent to \'<NAME> equ \"<VAL>\"\' in code");
Logger::printLine(L" -erroronwarning Treat all warnings like errors");
Logger::printLine(L" -temp <TEMP> Output temporary assembly data to <TEMP> file");
Logger::printLine(L" -sym <SYM> Output symbol data in the sym format to <SYM> file");
Logger::printLine(L" -sym2 <SYM2> Output symbol data in the sym2 format to <SYM2> file");
Logger::printLine(L" -root <ROOT> Use <ROOT> as working directory during execution");
Logger::printLine(L" -equ <NAME> <VAL> Equivalent to \'<NAME> equ <VAL>\' in code");
Logger::printLine(L" -strequ <NAME> <VAL> Equivalent to \'<NAME> equ \"<VAL>\"\' in code");
Logger::printLine(L" -definelabel <NAME> <VAL> Equivalent to \'.definelabel <NAME>, <VAL>\' in code");
Logger::printLine(L" -erroronwarning Treat all warnings like errors");
Logger::printLine(L"");
Logger::printLine(L"File arguments:");
Logger::printLine(L" <FILE> Main assembly code file");
Logger::printLine(L" <FILE> Main assembly code file");
}

int wmain(int argc, wchar_t* argv[])
@@ -98,6 +100,24 @@ int wmain(int argc, wchar_t* argv[])
{
changeDirectory(arguments[argpos + 1]);
argpos += 2;
} else if (arguments[argpos] == L"-definelabel" && argpos + 2 < arguments.size())
{
LabelDefinition def;

def.name = arguments[argpos + 1];
std::transform(def.name.begin(), def.name.end(), def.name.begin(), ::towlower);

int64_t value;
if (!stringToInt(arguments[argpos + 2], 0, arguments[argpos + 2].size(), value))
{
Logger::printError(Logger::Error, L"Invalid definelabel value '%s'\n", arguments[argpos + 2]);
printUsage(arguments[0]);
return 1;
}
def.value = value;

parameters.labels.push_back(def);
argpos += 3;
} else {
Logger::printError(Logger::Error, L"Invalid command line argument '%s'\n", arguments[argpos]);
printUsage(arguments[0]);
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "Tokenizer.h"
#include "Core/Common.h"
#include "Util/Util.h"
#include <algorithm>


@@ -433,57 +434,7 @@ bool FileTokenizer::parseOperator()

bool FileTokenizer::convertInteger(size_t start, size_t end, int64_t& result)
{
// find base of number
int32_t base = 10;
if (currentLine[start] == '0')
{
if (towlower(currentLine[start+1]) == 'x')
{
base = 16;
start += 2;
} else if (towlower(currentLine[start+1]) == 'o')
{
base = 8;
start += 2;
} else if (towlower(currentLine[start+1]) == 'b' && towlower(currentLine[end-1]) != 'h')
{
base = 2;
start += 2;
}
}

if (base == 10)
{
if (towlower(currentLine[end-1]) == 'h')
{
base = 16;
end--;
} else if (towlower(currentLine[end-1]) == 'b')
{
base = 2;
end--;
} else if (towlower(currentLine[end-1]) == 'o')
{
base = 8;
end--;
}
}

// convert number
result = 0;
while (start < end)
{
wchar_t c = towlower(currentLine[start++]);

int32_t value = c >= 'a' ? c-'a'+10 : c-'0';

if (value >= base)
return false;

result = (result*base) + value;
}

return true;
return stringToInt(currentLine, start, end, result);
}

bool FileTokenizer::convertFloat(size_t start, size_t end, double& result)
@@ -60,6 +60,9 @@ Equivalent to using `name equ replacement` in the assembly code.
#### `-strequ <name> <replacement>`
Equivalent to using `name equ "replacement"` in the assembly code.

#### `-definelabel <name> <replacement>`
Equivalent to using `.definelabel name, replacement` in the assembly code.

#### `-root <directory>`
Specifies the working directory to be used during execution.

@@ -141,6 +141,61 @@ std::wstring intToString(unsigned int value, int digits)
return result;
}

bool stringToInt(const std::wstring& line, size_t start, size_t end, int64_t& result)
{
// find base of number
int32_t base = 10;
if (line[start] == '0')
{
if (towlower(line[start+1]) == 'x')
{
base = 16;
start += 2;
} else if (towlower(line[start+1]) == 'o')
{
base = 8;
start += 2;
} else if (towlower(line[start+1]) == 'b' && towlower(line[end-1]) != 'h')
{
base = 2;
start += 2;
}
}

if (base == 10)
{
if (towlower(line[end-1]) == 'h')
{
base = 16;
end--;
} else if (towlower(line[end-1]) == 'b')
{
base = 2;
end--;
} else if (towlower(line[end-1]) == 'o')
{
base = 8;
end--;
}
}

// convert number
result = 0;
while (start < end)
{
wchar_t c = towlower(line[start++]);

int32_t value = c >= 'a' ? c-'a'+10 : c-'0';

if (value >= base)
return false;

result = (result*base) + value;
}

return true;
}

int32_t getFloatBits(float value)
{
union { float f; int32_t i; } u;
@@ -12,6 +12,7 @@ std::string convertWCharToUtf8(wchar_t character);

std::wstring intToHexString(unsigned int value, int digits, bool prefix = false);
std::wstring intToString(unsigned int value, int digits);
bool stringToInt(const std::wstring& line, size_t start, size_t end, int64_t& result);
int32_t getFloatBits(float value);
int64_t getDoubleBits(double value);

0 comments on commit d7e3ba4

Please sign in to comment.