From 6ca8cf9d9a81e98e615ff2088ee5ec74f8f59d47 Mon Sep 17 00:00:00 2001 From: Jonathan-R0 Date: Sat, 14 Nov 2020 23:12:35 -0300 Subject: [PATCH] Refactor del parser, evito aun mas allocs --- asmline.cpp | 14 +++++----- asmline.h | 1 + graphFiller.cpp | 2 -- parser.cpp | 74 ++++++++++++++++--------------------------------- parser.h | 5 ---- 5 files changed, 32 insertions(+), 64 deletions(-) diff --git a/asmline.cpp b/asmline.cpp index 5db5edc..17126b8 100644 --- a/asmline.cpp +++ b/asmline.cpp @@ -3,13 +3,9 @@ #include #include -Asmline::Asmline() { - label = {}; - opCode = {}; - labelsToJump = {}; - jumpCodes = {"jmp", "ja", "jeq", "jneq", "jne", - "jlt", "jle", "jgt", "jge", "jset"}; -} +Asmline::Asmline() + : jumpCodes({"jmp", "ja", "jeq", "jneq", "jne", "jlt", "jle", "jgt", "jge", + "jset"}) {} bool Asmline::isJump() { return (find(jumpCodes.begin(), jumpCodes.end(), opCode) != jumpCodes.end()); @@ -29,6 +25,10 @@ std::list Asmline::getLabelsToJumpTo() { } void Asmline::setLabel(std::string labelGiven) { label = labelGiven; } void Asmline::setOpCode(std::string opCodeGiven) { opCode = opCodeGiven; } +void Asmline::setLabelToJump(std::string labelToJumpGiven) { + labelsToJump.push_back(labelToJumpGiven); +} + void Asmline::setLabelsToJump(std::list labelsToJumpGiven) { if (!this->isJump()) return; if (labelsToJumpGiven.size() == 1) { diff --git a/asmline.h b/asmline.h index 3d17cfc..3ad5ad9 100644 --- a/asmline.h +++ b/asmline.h @@ -15,6 +15,7 @@ class Asmline { Asmline(); void setLabel(std::string labelGiven); void setOpCode(std::string opCodeGiven); + void setLabelToJump(std::string labelToJumpGiven); std::string getOpcode(); std::string getLabel(); std::list getLabelsToJumpTo(); diff --git a/graphFiller.cpp b/graphFiller.cpp index 3f4f9a7..b79b551 100644 --- a/graphFiller.cpp +++ b/graphFiller.cpp @@ -18,12 +18,10 @@ Graphfiller::Graphfiller() { void Graphfiller::addInstructionToGraph(std::string line, int lineNumber) { if (line.size() == 0) return; - Asmline instruction; parser.parseInstruction(std::move(line), instruction); opGraph.addVertex(lineNumber); if (lineNumber != 1) opGraph.connectLast(lineNumber); - std::list labelsToJump = instruction.getLabelsToJumpTo(); if (instruction.esCortante()) aristaACortar.push_front(lineNumber); if (instruction.getLabel().size() != 0) diff --git a/parser.cpp b/parser.cpp index 9764a6a..838fd19 100644 --- a/parser.cpp +++ b/parser.cpp @@ -3,18 +3,6 @@ #include #include -#define SUCCESS 0 -#define FAILURE (SUCCESS - 1) - -Parser::Parser() : delims({':', ',', ' ', '\n', '\t', '\0'}) {} - -bool Parser::isDelim(char c) { - for (auto i : delims) { - if (i == c) return true; - } - return false; -} - static void parseSpaces(std::string* asmLine) { std::string noSpacesExtra = {}; char anterior = 0; @@ -22,50 +10,36 @@ static void parseSpaces(std::string* asmLine) { if (!(c == ' ' && anterior == ' ')) noSpacesExtra.push_back(c); anterior = c; } + if (noSpacesExtra.front() == ' ') noSpacesExtra.erase(0, 1); *asmLine = noSpacesExtra; } -static void parseLabel(Asmline& instruction, bool hasLabel, - std::list& parts) { - if (parts.size() == 0) return; - if (hasLabel) { - instruction.setLabel(std::move(parts.front())); - parts.pop_front(); +void Parser::parseInstruction(std::string asmLine, Asmline& instruction) { + parseSpaces(&asmLine); + int firstDots = asmLine.find_first_of(":"); + if (firstDots != -1) { + instruction.setLabel(asmLine.substr(0, firstDots)); + asmLine.erase(0, firstDots + 1); + int firstNonSpace = asmLine.find_first_not_of(" "); + asmLine.erase(0, firstNonSpace); } -} -static int parseData(Parser* p, std::string asmLine, bool& hasLabel, - std::list& parts, Asmline& instruction) { - std::string placeHolder; - char lastChar = 0; - for (auto c : asmLine) { - lastChar = c; - if (p->isDelim(c)) { - if (c == ':') hasLabel = true; - if (placeHolder.size() != 0) { - parts.push_back(placeHolder); - } - if (c != '\n') placeHolder.erase(); - } else if (!p->isDelim(c)) { - placeHolder.push_back(c); - } + int firstSpace = asmLine.find_first_of(" "); + if (firstSpace != -1) { + instruction.setOpCode(asmLine.substr(0, firstSpace)); + asmLine.erase(0, firstSpace + 1); } - if (!p->isDelim(lastChar)) parts.push_back(placeHolder); - if (parts.size() == 0) return FAILURE; // Empty line. - parseLabel(instruction, hasLabel, parts); - return SUCCESS; -} - -void Parser::parseInstruction(std::string asmLine, Asmline& instruction) { - bool hasLabel = false; - std::list parts; - parseSpaces(&asmLine); - - if (parseData(this, std::move(asmLine), hasLabel, parts, instruction) == - SUCCESS) { - instruction.setOpCode(std::move(parts.front())); - parts.pop_front(); - instruction.setLabelsToJump(std::move(parts)); + if (instruction.isJump()) { + int firstComma = asmLine.find_first_of(","); + std::string firstArg = asmLine.substr(0, firstComma); + asmLine.erase(0, firstComma + 2); + if ((firstComma = asmLine.find_first_of(",")) != -1) { + instruction.setLabelToJump(asmLine.substr(0, firstComma)); + asmLine.erase(0, firstComma + 2); + if (!asmLine.empty()) instruction.setLabelToJump(asmLine); + } else { + instruction.setLabelToJump(asmLine); + } } } diff --git a/parser.h b/parser.h index 70ec8c5..5d2e664 100644 --- a/parser.h +++ b/parser.h @@ -6,13 +6,8 @@ #include "asmline.h" class Parser { - private: - std::list delims; - public: - Parser(); void parseInstruction(std::string asmLine, Asmline& instruction); - bool isDelim(char c); }; #endif // PARSER_H_