Permalink
Browse files

Use smart pointers for symbol labels and assembler commands

  • Loading branch information...
sp1187 committed Jun 22, 2018
1 parent 2f9b41d commit 85f44a8a6b7e70de8e352adfcece73a7f5bdc563
View
@@ -17,14 +17,14 @@ CArmArchitecture::~CArmArchitecture()
clear();
}
CAssemblerCommand* CArmArchitecture::parseDirective(Parser& parser)
std::unique_ptr<CAssemblerCommand> CArmArchitecture::parseDirective(Parser& parser)
{
ArmParser armParser;
return armParser.parseDirective(parser);
}
CAssemblerCommand* CArmArchitecture::parseOpcode(Parser& parser)
std::unique_ptr<CAssemblerCommand> CArmArchitecture::parseOpcode(Parser& parser)
{
ArmParser armParser;
@@ -68,7 +68,7 @@ void CArmArchitecture::NextSection()
std::unique_ptr<IElfRelocator> CArmArchitecture::getElfRelocator()
{
return std::unique_ptr<IElfRelocator>(new ArmElfRelocator(version != AARCH_GBA));
return make_unique<ArmElfRelocator>(version != AARCH_GBA);
}
void CArmArchitecture::addPoolValue(ArmOpcodeCommand* command, int32_t value)
View
@@ -35,8 +35,8 @@ class CArmArchitecture: public CArchitecture
~CArmArchitecture();
void clear();
virtual CAssemblerCommand* parseDirective(Parser& parser);
virtual CAssemblerCommand* parseOpcode(Parser& parser);
virtual std::unique_ptr<CAssemblerCommand> parseDirective(Parser& parser);
virtual std::unique_ptr<CAssemblerCommand> parseOpcode(Parser& parser);
virtual const ExpressionFunctionMap& getExpressionFunctions();
virtual void NextSection();
virtual void Pass2();
@@ -196,7 +196,7 @@ const wchar_t* ctorTemplate =
L".word %ctorContent%"
;
CAssemblerCommand* ArmElfRelocator::generateCtorStub(std::vector<ElfRelocatorCtor>& ctors)
std::unique_ptr<CAssemblerCommand> ArmElfRelocator::generateCtorStub(std::vector<ElfRelocatorCtor>& ctors)
{
std::wstring ctorText;
@@ -22,7 +22,7 @@ class ArmElfRelocator: public IElfRelocator
virtual bool relocateOpcode(int type, RelocationData& data);
virtual void setSymbolAddress(RelocationData& data, int64_t symbolAddress, int symbolType);
virtual CAssemblerCommand* generateCtorStub(std::vector<ElfRelocatorCtor>& ctors);
virtual std::unique_ptr<CAssemblerCommand> generateCtorStub(std::vector<ElfRelocatorCtor>& ctors);
private:
bool arm9;
};
View
@@ -31,23 +31,23 @@ const ArmRegisterDescriptor armCopNumbers[] = {
{ L"p12", 12 }, { L"p13", 13 }, { L"p14", 14 }, { L"p15", 15 },
};
CAssemblerCommand* parseDirectiveThumb(Parser& parser, int flags)
std::unique_ptr<CAssemblerCommand> parseDirectiveThumb(Parser& parser, int flags)
{
Arm.SetThumbMode(true);
return new ArmStateCommand(false);
return make_unique<ArmStateCommand>(false);
}
CAssemblerCommand* parseDirectiveArm(Parser& parser, int flags)
std::unique_ptr<CAssemblerCommand> parseDirectiveArm(Parser& parser, int flags)
{
Arm.SetThumbMode(false);
return new ArmStateCommand(true);
return make_unique<ArmStateCommand>(true);
}
CAssemblerCommand* parseDirectivePool(Parser& parser, int flags)
std::unique_ptr<CAssemblerCommand> parseDirectivePool(Parser& parser, int flags)
{
CommandSequence* seq = new CommandSequence();
seq->addCommand(new CDirectiveAlignFill(4,CDirectiveAlignFill::Align));
seq->addCommand(new ArmPoolCommand());
auto seq = make_unique<CommandSequence>();
seq->addCommand(make_unique<CDirectiveAlignFill>(4,CDirectiveAlignFill::Align));
seq->addCommand(make_unique<ArmPoolCommand>());
return seq;
}
@@ -61,7 +61,7 @@ const wchar_t* msgTemplate =
L"%after%:"
;
CAssemblerCommand* parseDirectiveMsg(Parser& parser, int flags)
std::unique_ptr<CAssemblerCommand> parseDirectiveMsg(Parser& parser, int flags)
{
Expression text = parser.parseExpression();
if (text.isLoaded() == false)
@@ -81,7 +81,7 @@ const DirectiveMap armDirectives = {
{ L".msg", { &parseDirectiveMsg, 0 } },
};
CAssemblerCommand* ArmParser::parseDirective(Parser& parser)
std::unique_ptr<CAssemblerCommand> ArmParser::parseDirective(Parser& parser)
{
return parser.parseDirective(armDirectives);
}
@@ -629,7 +629,7 @@ bool ArmParser::parseArmParameters(Parser& parser, const tArmOpcode& opcode, Arm
return parser.nextToken().type == TokenType::Separator;
}
CArmInstruction* ArmParser::parseArmOpcode(Parser& parser)
std::unique_ptr<CArmInstruction> ArmParser::parseArmOpcode(Parser& parser)
{
if (parser.peekToken().type != TokenType::Identifier)
return nullptr;
@@ -652,7 +652,7 @@ CArmInstruction* ArmParser::parseArmOpcode(Parser& parser)
if (parseArmParameters(parser,ArmOpcodes[z],vars) == true)
{
// success, return opcode
return new CArmInstruction(ArmOpcodes[z],vars);
return make_unique<CArmInstruction>(ArmOpcodes[z],vars);
}
parser.getTokenizer()->setPosition(tokenPos);
@@ -726,7 +726,7 @@ bool ArmParser::parseThumbParameters(Parser& parser, const tThumbOpcode& opcode,
return parser.nextToken().type == TokenType::Separator;
}
CThumbInstruction* ArmParser::parseThumbOpcode(Parser& parser)
std::unique_ptr<CThumbInstruction> ArmParser::parseThumbOpcode(Parser& parser)
{
if (parser.peekToken().type != TokenType::Identifier)
return nullptr;
@@ -752,7 +752,7 @@ CThumbInstruction* ArmParser::parseThumbOpcode(Parser& parser)
if (parseThumbParameters(parser,ThumbOpcodes[z],vars) == true)
{
// success, return opcode
return new CThumbInstruction(ThumbOpcodes[z],vars);
return make_unique<CThumbInstruction>(ThumbOpcodes[z],vars);
}
parser.getTokenizer()->setPosition(tokenPos);
View
@@ -13,9 +13,9 @@ struct ArmRegisterDescriptor {
class ArmParser
{
public:
CAssemblerCommand* parseDirective(Parser& parser);
CArmInstruction* parseArmOpcode(Parser& parser);
CThumbInstruction* parseThumbOpcode(Parser& parser);
std::unique_ptr<CAssemblerCommand> parseDirective(Parser& parser);
std::unique_ptr<CArmInstruction> parseArmOpcode(Parser& parser);
std::unique_ptr<CThumbInstruction> parseThumbOpcode(Parser& parser);
private:
bool parseRegisterTable(Parser& parser, ArmRegisterValue& dest, const ArmRegisterDescriptor* table, size_t count);
bool parseRegister(Parser& parser, ArmRegisterValue& dest, int max = 15);
@@ -40,4 +40,4 @@ class ArmParser
bool parseArmParameters(Parser& parser, const tArmOpcode& opcode, ArmOpcodeVariables& vars);
bool parseThumbParameters(Parser& parser, const tThumbOpcode& opcode, ThumbOpcodeVariables& vars);
};
};
View
@@ -10,8 +10,8 @@ class Parser;
class CArchitecture
{
public:
virtual CAssemblerCommand* parseDirective(Parser& parser) { return nullptr; }
virtual CAssemblerCommand* parseOpcode(Parser& parser) { return nullptr; }
virtual std::unique_ptr<CAssemblerCommand> parseDirective(Parser& parser) { return nullptr; }
virtual std::unique_ptr<CAssemblerCommand> parseOpcode(Parser& parser) { return nullptr; }
virtual const ExpressionFunctionMap& getExpressionFunctions() { return emptyMap; }
virtual void NextSection() = 0;
virtual void Pass2() = 0;
View
@@ -16,17 +16,17 @@ CMipsArchitecture::CMipsArchitecture()
Version = MARCH_INVALID;
}
CAssemblerCommand* CMipsArchitecture::parseDirective(Parser& parser)
std::unique_ptr<CAssemblerCommand> CMipsArchitecture::parseDirective(Parser& parser)
{
MipsParser mipsParser;
return mipsParser.parseDirective(parser);
}
CAssemblerCommand* CMipsArchitecture::parseOpcode(Parser& parser)
std::unique_ptr<CAssemblerCommand> CMipsArchitecture::parseOpcode(Parser& parser)
{
MipsParser mipsParser;
CAssemblerCommand* macro = mipsParser.parseMacro(parser);
std::unique_ptr<CAssemblerCommand> macro = mipsParser.parseMacro(parser);
if (macro != nullptr)
return macro;
@@ -59,7 +59,7 @@ std::unique_ptr<IElfRelocator> CMipsArchitecture::getElfRelocator()
case MARCH_PS2:
case MARCH_PSP:
case MARCH_N64:
return std::unique_ptr<IElfRelocator>(new MipsElfRelocator());
return make_unique<MipsElfRelocator>();
case MARCH_PSX:
case MARCH_RSP:
default:
View
@@ -8,8 +8,8 @@ class CMipsArchitecture: public CArchitecture
{
public:
CMipsArchitecture();
virtual CAssemblerCommand* parseDirective(Parser& parser);
virtual CAssemblerCommand* parseOpcode(Parser& parser);
virtual std::unique_ptr<CAssemblerCommand> parseDirective(Parser& parser);
virtual std::unique_ptr<CAssemblerCommand> parseOpcode(Parser& parser);
virtual const ExpressionFunctionMap& getExpressionFunctions();
virtual void NextSection();
virtual void Pass2() { return; };
@@ -74,7 +74,7 @@ const wchar_t* mipsCtorTemplate = LR"(
.word %ctorContent%
)";
CAssemblerCommand* MipsElfRelocator::generateCtorStub(std::vector<ElfRelocatorCtor>& ctors)
std::unique_ptr<CAssemblerCommand> MipsElfRelocator::generateCtorStub(std::vector<ElfRelocatorCtor>& ctors)
{
Parser parser;
if (ctors.size() != 0)
@@ -23,5 +23,5 @@ class MipsElfRelocator: public IElfRelocator
virtual int expectedMachine() const;
virtual bool relocateOpcode(int type, RelocationData& data);
virtual void setSymbolAddress(RelocationData& data, int64_t symbolAddress, int symbolType);
virtual CAssemblerCommand* generateCtorStub(std::vector<ElfRelocatorCtor>& ctors);
virtual std::unique_ptr<CAssemblerCommand> generateCtorStub(std::vector<ElfRelocatorCtor>& ctors);
};
View
@@ -7,18 +7,13 @@
#include "Parser/Parser.h"
#include "MipsParser.h"
MipsMacroCommand::MipsMacroCommand(CAssemblerCommand* content, int macroFlags)
MipsMacroCommand::MipsMacroCommand(std::unique_ptr<CAssemblerCommand> content, int macroFlags)
{
this->content = content;
this->content = std::move(content);
this->macroFlags = macroFlags;
IgnoreLoadDelay = Mips.GetIgnoreDelay();
}
MipsMacroCommand::~MipsMacroCommand()
{
delete content;
}
bool MipsMacroCommand::Validate()
{
int64_t memoryPos = g_fileManager->getVirtualAddress();
@@ -64,13 +59,13 @@ std::wstring preprocessMacro(const wchar_t* text, MipsImmediateData& immediates)
return formatString(L"%s: %s",labelName,text);
}
CAssemblerCommand* createMacro(Parser& parser, const std::wstring& text, int flags, std::initializer_list<AssemblyTemplateArgument> variables)
std::unique_ptr<CAssemblerCommand> createMacro(Parser& parser, const std::wstring& text, int flags, std::initializer_list<AssemblyTemplateArgument> variables)
{
CAssemblerCommand* content = parser.parseTemplate(text,variables);
return new MipsMacroCommand(content,flags);
std::unique_ptr<CAssemblerCommand> content = parser.parseTemplate(text,variables);
return make_unique<MipsMacroCommand>(std::move(content),flags);
}
CAssemblerCommand* generateMipsMacroAbs(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroAbs(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* templateAbs = LR"(
%sraop% r1,%rs%,31
@@ -96,7 +91,7 @@ CAssemblerCommand* generateMipsMacroAbs(Parser& parser, MipsRegisterData& regist
});
}
CAssemblerCommand* generateMipsMacroLiFloat(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroLiFloat(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* templateLiFloat = LR"(
li r1,float(%imm%)
@@ -112,7 +107,7 @@ CAssemblerCommand* generateMipsMacroLiFloat(Parser& parser, MipsRegisterData& re
});
}
CAssemblerCommand* generateMipsMacroLi(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroLi(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* templateLi = LR"(
.if abs(%imm%) > 0xFFFFFFFF
@@ -163,7 +158,7 @@ CAssemblerCommand* generateMipsMacroLi(Parser& parser, MipsRegisterData& registe
});
}
CAssemblerCommand* generateMipsMacroLoadStore(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroLoadStore(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* templateLoadStore = LR"(
.if %imm% & ~0xFFFFFFFF
@@ -227,7 +222,7 @@ CAssemblerCommand* generateMipsMacroLoadStore(Parser& parser, MipsRegisterData&
});
}
CAssemblerCommand* generateMipsMacroLoadUnaligned(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroLoadUnaligned(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* selectedTemplate;
@@ -262,7 +257,7 @@ CAssemblerCommand* generateMipsMacroLoadUnaligned(Parser& parser, MipsRegisterDa
if (registers.grs.num == registers.grd.num)
{
Logger::printError(Logger::Error,L"Cannot use same register as source and destination");
return new DummyCommand();
return make_unique<DummyCommand>();
}
op = type == MIPSM_W ? L"lw" : L"ld";
@@ -282,7 +277,7 @@ CAssemblerCommand* generateMipsMacroLoadUnaligned(Parser& parser, MipsRegisterDa
});
}
CAssemblerCommand* generateMipsMacroStoreUnaligned(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroStoreUnaligned(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* selectedTemplate;
@@ -315,7 +310,7 @@ CAssemblerCommand* generateMipsMacroStoreUnaligned(Parser& parser, MipsRegisterD
if (registers.grs.num == registers.grd.num)
{
Logger::printError(Logger::Error,L"Cannot use same register as source and destination");
return new DummyCommand();
return make_unique<DummyCommand>();
}
op = type == MIPSM_W ? L"sw" : L"sd";
@@ -335,7 +330,7 @@ CAssemblerCommand* generateMipsMacroStoreUnaligned(Parser& parser, MipsRegisterD
});
}
CAssemblerCommand* generateMipsMacroBranch(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroBranch(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* selectedTemplate;
@@ -421,7 +416,7 @@ CAssemblerCommand* generateMipsMacroBranch(Parser& parser, MipsRegisterData& reg
});
}
CAssemblerCommand* generateMipsMacroSet(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroSet(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
const wchar_t* selectedTemplate;
@@ -514,7 +509,7 @@ CAssemblerCommand* generateMipsMacroSet(Parser& parser, MipsRegisterData& regist
});
}
CAssemblerCommand* generateMipsMacroRotate(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
std::unique_ptr<CAssemblerCommand> generateMipsMacroRotate(Parser& parser, MipsRegisterData& registers, MipsImmediateData& immediates, int flags)
{
bool left = (flags & MIPSM_LEFT) != 0;
bool immediate = (flags & MIPSM_IMM) != 0;
View
@@ -39,7 +39,7 @@
class Parser;
typedef CAssemblerCommand* (*MipsMacroFunc)(Parser&,MipsRegisterData&,MipsImmediateData&,int);
using MipsMacroFunc = std::unique_ptr<CAssemblerCommand> (*)(Parser&,MipsRegisterData&,MipsImmediateData&,int);
struct MipsMacroDefinition {
const wchar_t* name;
@@ -53,13 +53,12 @@ extern const MipsMacroDefinition mipsMacros[];
class MipsMacroCommand: public CAssemblerCommand
{
public:
MipsMacroCommand(CAssemblerCommand* content, int macroFlags);
~MipsMacroCommand();
MipsMacroCommand(std::unique_ptr<CAssemblerCommand> content, int macroFlags);
virtual bool Validate();
virtual void Encode() const;
virtual void writeTempData(TempData& tempData) const;
private:
CAssemblerCommand* content;
std::unique_ptr<CAssemblerCommand> content;
int macroFlags;
bool IgnoreLoadDelay;
};
Oops, something went wrong.

0 comments on commit 85f44a8

Please sign in to comment.