From 9841216d742f049a07d60253abd79eaa2bb977bf Mon Sep 17 00:00:00 2001 From: park671 Date: Tue, 26 Nov 2024 11:55:04 +0800 Subject: [PATCH] update: replace stl with C impl --- CMakeLists.txt | 8 ++ compiler/lexer.cpp | 3 - compiler/syntaxer.cpp | 204 ++++++++++++++++++++-------- generator/arm64/assembler_arm64.cpp | 99 +++++++++----- memory/mspace.cpp | 2 - memory/mspace.h | 8 ++ utils/stack.c | 98 +++++++++++++ utils/stack.h | 41 ++++++ 8 files changed, 366 insertions(+), 97 deletions(-) create mode 100644 utils/stack.c create mode 100644 utils/stack.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 40370aa..9cbdb41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,13 @@ include_directories( "compiler" "generator" "wrapper" + "utils" +) + +file(GLOB_RECURSE UTILS_SRC + "utils/*.cc" + "utils/*.c" + "utils/*.cpp" ) file(GLOB_RECURSE LOGGER_SRC @@ -66,6 +73,7 @@ add_executable(pcc ${COMPILER_SRC} ${GENERATOR_SRC} ${WRAPPER_SRC} + ${UTILS_SRC} generator/arm64/register_arm64.h generator/assembler.cpp generator/assembler.h diff --git a/compiler/lexer.cpp b/compiler/lexer.cpp index e6d16f1..0ec9337 100644 --- a/compiler/lexer.cpp +++ b/compiler/lexer.cpp @@ -5,11 +5,8 @@ #include #include "lexer.h" #include "logger.h" -#include #include "mspace.h" -using namespace std; - static const char *LEXER_TAG = "lexer"; static const char *VAR_TAG = "var"; diff --git a/compiler/syntaxer.cpp b/compiler/syntaxer.cpp index 00202ae..15c43fb 100644 --- a/compiler/syntaxer.cpp +++ b/compiler/syntaxer.cpp @@ -1,65 +1,145 @@ // // Created by Park Yu on 2024/9/11. // - -#include -#include +#include #include "syntaxer.h" #include "logger.h" #include "string.h" #include "mspace.h" -using namespace std; - //16KB #define BUFFER_SIZE 16384 #define SYNTAX_TAG "syntaxer" -static stack *> varList; -static vector methodList; +struct VarListNode { + AstIdentity *identity; + VarListNode *next; +}; + +struct VarStackNode { + VarListNode *varListHead; + VarStackNode *next; +}; + +struct MethodListNode { + AstMethodDefine *methodDefine; + MethodListNode *next; +}; + +static VarStackNode *varStackHead; +static MethodListNode *methodListHead; inline static bool hasVarDefine(const char *name) { - vector *currentMethodStack = varList.top(); - for (int i = 0; i < currentMethodStack->size(); i++) { - if (strcmp((*currentMethodStack)[i]->name, name) == 0) { - return true; + VarStackNode *stackNode = varStackHead; + while (stackNode != nullptr) { + VarListNode *p = stackNode->varListHead; + while (p != nullptr) { + if (strcmp(p->identity->name, name) == 0) { + return true; + } + p = p->next; } + stackNode = stackNode->next; } return false; } inline static bool hasMethodDefine(const char *name) { - for (int i = 0; i < methodList.size(); i++) { - if (strcmp(methodList[i]->identity->name, name) == 0) { + MethodListNode *p = methodListHead; + while (p != nullptr) { + if (strcmp(p->methodDefine->identity->name, name) == 0) { return true; } + p = p->next; } return false; } inline static AstMethodDefine *getMethodDefine(const char *name) { - for (int i = 0; i < methodList.size(); i++) { - if (strcmp(methodList[i]->identity->name, name) == 0) { - return methodList[i]; + MethodListNode *p = methodListHead; + while (p != nullptr) { + if (strcmp(p->methodDefine->identity->name, name) == 0) { + return p->methodDefine; } + p = p->next; } return nullptr; } inline static void pushMethod(AstMethodDefine *astMethodDefine) { - methodList.push_back(astMethodDefine); - vector *methodStack = new vector(); - varList.push(methodStack); + MethodListNode *methodListNode = (MethodListNode *) pccMalloc(SYNTAX_TAG, sizeof(MethodListNode)); + methodListNode->next = nullptr; + methodListNode->methodDefine = astMethodDefine; + + if (methodListHead == nullptr) { + methodListHead = methodListNode; + } else { + MethodListNode *p = methodListHead; + while (p->next != nullptr) { + p = p->next; + } + p->next = methodListNode; + } + + VarStackNode *stackNode = (VarStackNode *) pccMalloc(SYNTAX_TAG, sizeof(VarStackNode)); + stackNode->varListHead = nullptr; + stackNode->next = nullptr; + if (varStackHead == nullptr) { + varStackHead = stackNode; + } else { + VarStackNode *stackTop = varStackHead; + while (stackTop->next != nullptr) { + stackTop = stackTop->next; + } + stackTop->next = stackNode; + } +} + +inline static void freeVarStack(VarStackNode *varStackNode) { + if (varStackNode->next != nullptr) { + loge(SYNTAX_TAG, "free non top node!"); + exit(-1); + } + VarListNode *varListNode = varStackNode->varListHead; + while (varListNode != nullptr) { + VarListNode *next = varListNode->next; + pccFree(SYNTAX_TAG, varListNode); + varListNode = next; + } + pccFree(SYNTAX_TAG, varStackNode); } inline static void popMethod() { - vector *methodStack = varList.top(); - delete methodStack; - varList.pop(); + VarStackNode *stackTop = varStackHead; + if (stackTop->next == nullptr) { + freeVarStack(varStackHead); + varStackHead = nullptr; + } else { + while (stackTop->next->next != nullptr) { + stackTop = stackTop->next; + } + freeVarStack(stackTop->next); + stackTop->next = nullptr; + } } inline static void addVar(AstIdentity *astIdentity) { - varList.top()->push_back(astIdentity); + VarStackNode *stackTop = varStackHead; + while (stackTop->next != nullptr) { + stackTop = stackTop->next; + } + VarListNode *varListNode = (VarListNode *) pccMalloc(SYNTAX_TAG, sizeof(VarListNode)); + varListNode->next = nullptr; + varListNode->identity = astIdentity; + if (stackTop->varListHead == nullptr) { + stackTop->varListHead = varListNode; + } else { + VarListNode *varListTail = stackTop->varListHead; + while (varListTail->next != nullptr) { + varListTail = varListTail->next; + } + varListTail->next = varListNode; + } } inline static PrimitiveType convertTokenType2PrimitiveType(const char *type) { @@ -117,7 +197,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { //method type if (token->tokenType != TOKEN_TYPE && token->tokenType != TOKEN_POINTER_TYPE) { loge(SYNTAX_TAG, "[-]error: method define need type: %s", token->content); - return nullptr; + exit(-1); } astMethodDefine->type = (AstType *) pccMalloc(SYNTAX_TAG, sizeof(AstType)); astMethodDefine->type->isPointer = (token->tokenType == TOKEN_POINTER_TYPE); @@ -126,7 +206,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { //method name if (token->tokenType != TOKEN_IDENTIFIER) { loge(SYNTAX_TAG, "[-]error: method define need identifier: %s", token->content); - return nullptr; + exit(-1); } astMethodDefine->identity = (AstIdentity *) pccMalloc(SYNTAX_TAG, sizeof(AstIdentity)); astMethodDefine->identity->name = token->content; @@ -135,7 +215,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { //method ( if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "(") != 0) { loge(SYNTAX_TAG, "[-]error: method define need (: %s", token->content); - return nullptr; + exit(-1); } token = token->next; pushMethod(astMethodDefine); @@ -149,21 +229,25 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { //method ) if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ")") != 0) { loge(SYNTAX_TAG, "[-]error: method define need ): %s", token->content); - return nullptr; + exit(-1); } token = token->next; - if (astMethodDefine->defineType == METHOD_EXTERN) { + // method code block "{}" + if (token->tokenType == TOKEN_BOUNDARY && strcmp(token->content, ";") == 0) { + //extern a method without "extern" keyword + astMethodDefine->defineType = METHOD_EXTERN; astMethodDefine->statementBlock = nullptr; - if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ";") != 0) { - loge(SYNTAX_TAG, "[-]error: method define need \";\" at end: %s", token->content); - return nullptr; - } token = token->next; } else { - //method code block - astMethodDefine->statementBlock = (AstStatementBlock *) pccMalloc(SYNTAX_TAG, - sizeof(AstStatementBlock)); - token = travelAst(token, astMethodDefine->statementBlock, NODE_STATEMENT_BLOCK); + if (astMethodDefine->defineType == METHOD_EXTERN) { + loge(SYNTAX_TAG, "[-]error: method define need \";\" at end: %s", token->content); + exit(-1); + } else { + //method code block + astMethodDefine->statementBlock = (AstStatementBlock *) pccMalloc(SYNTAX_TAG, + sizeof(AstStatementBlock)); + token = travelAst(token, astMethodDefine->statementBlock, NODE_STATEMENT_BLOCK); + } } popMethod(); break; @@ -186,7 +270,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { AstParamDefine *astParamDefine = (AstParamDefine *) currentNode; if (token->tokenType != TOKEN_TYPE && token->tokenType != TOKEN_POINTER_TYPE) { loge(SYNTAX_TAG, "[-]error: param define need type: %s", token->content); - return nullptr; + exit(-1); } astParamDefine->type = (AstType *) pccMalloc(SYNTAX_TAG, sizeof(AstType)); astParamDefine->type->primitiveType = convertTokenType2PrimitiveType(token->content); @@ -194,7 +278,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = token->next; if (token->tokenType != TOKEN_IDENTIFIER) { loge(SYNTAX_TAG, "[-]error: param define need identifier: %s", token->content); - return nullptr; + exit(-1); } astParamDefine->identity = (AstIdentity *) pccMalloc(SYNTAX_TAG, sizeof(AstIdentity)); astParamDefine->identity->name = token->content; @@ -208,7 +292,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { //block { if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "{") != 0) { loge(SYNTAX_TAG, "[-]error: code block define need {: %s", token->content); - return nullptr; + exit(-1); } token = token->next; //statement seq @@ -221,7 +305,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { //block } if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "}") != 0) { loge(SYNTAX_TAG, "[-]error: code block define need }: %s", token->content); - return nullptr; + exit(-1); } token = token->next; break; @@ -278,7 +362,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = token->next; if (token->tokenType != TOKEN_IDENTIFIER) { loge(SYNTAX_TAG, "[-]error: var define need identifier: %s", token->content); - return nullptr; + exit(-1); } astStatement->defineStatement->identity = (AstIdentity *) pccMalloc(SYNTAX_TAG, sizeof(AstIdentity)); astStatement->defineStatement->identity->name = token->content; @@ -287,7 +371,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = token->next; if (token->tokenType != TOKEN_OPERATOR || strcmp(token->content, "=") != 0) { loge(SYNTAX_TAG, "[-]error: var define need init fromValue: %s", token->content); - return nullptr; + exit(-1); } //consume = token = token->next; @@ -376,7 +460,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = token->next; if (token->tokenType != TOKEN_OPERATOR || strcmp(token->content, "=") != 0) { loge(SYNTAX_TAG, "[-]error: var assignment need fromValue: %s", token->content); - return nullptr; + exit(-1); } //consume = token = token->next; @@ -402,7 +486,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { AstStatementIf *astStatementIf = (AstStatementIf *) currentNode; if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "(") != 0) { loge(SYNTAX_TAG, "[-]error: need (: %s", token->content); - return nullptr; + exit(-1); } //consume ( token = token->next; @@ -410,7 +494,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = travelAst(token, astStatementIf->expression, NODE_EXPRESSION_BOOL); if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ")") != 0) { loge(SYNTAX_TAG, "[-]error: need ): %s", token->content); - return nullptr; + exit(-1); } //consume ) token = token->next; @@ -430,7 +514,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { AstStatementWhile *astStatementWhile = (AstStatementWhile *) currentNode; if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "(") != 0) { loge(SYNTAX_TAG, "[-]error: need (: %s", token->content); - return nullptr; + exit(-1); } //consume ( token = token->next; @@ -438,7 +522,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = travelAst(token, astStatementWhile->expression, NODE_EXPRESSION_BOOL); if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ")") != 0) { loge(SYNTAX_TAG, "[-]error: need ): %s", token->content); - return nullptr; + exit(-1); } //consume ) token = token->next; @@ -450,7 +534,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { AstStatementFor *astStatementFor = (AstStatementFor *) currentNode; if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "(") != 0) { loge(SYNTAX_TAG, "[-]error: need (: %s", token->content); - return nullptr; + exit(-1); } //consume ( token = token->next; @@ -462,7 +546,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { } if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ";") != 0) { loge(SYNTAX_TAG, "[-]error: for 1st need ;: %s", token->content); - return nullptr; + exit(-1); } //consume ; token = token->next; @@ -475,7 +559,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { } if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ";") != 0) { loge(SYNTAX_TAG, "[-]error: for 2nd need ;: %s", token->content); - return nullptr; + exit(-1); } //consume ; token = token->next; @@ -487,7 +571,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { } if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ")") != 0) { loge(SYNTAX_TAG, "[-]error: need ): %s", token->content); - return nullptr; + exit(-1); } //consume ) token = token->next; @@ -507,7 +591,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { } if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, ";") != 0) { loge(SYNTAX_TAG, "[-]error: return need ;: %s", token->content); - return nullptr; + exit(-1); } //consume ; token = token->next; @@ -621,7 +705,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { //do not consume method call's ; } else { loge(SYNTAX_TAG, "[-]error: undefined method: %s", token->content); - return nullptr; + exit(-1); } } else { if (hasVarDefine(token->content)) { @@ -632,7 +716,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = token->next; } else { loge(SYNTAX_TAG, "[-]error: undefined var: %s", token->content); - return nullptr; + exit(-1); } } } else if (token->tokenType == TOKEN_INTEGER || token->tokenType == TOKEN_FLOAT) { @@ -642,7 +726,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = travelAst(token, astArithmeticFactor->primitiveData, NODE_PRIMITIVE_DATA); } else if (token->tokenType == TOKEN_BOUNDARY && strcmp(token->content, "(") == 0) { loge(SYNTAX_TAG, "[-]error: not impl yet: %s", token->content); - return nullptr; + exit(-1); } else if (token->tokenType == TOKEN_BOUNDARY && strcmp(token->content, "{") == 0) { //array, consume { token = token->next; @@ -681,7 +765,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { astArithmeticFactor->identity = (AstIdentity *) pccMalloc(SYNTAX_TAG, sizeof(AstIdentity)); astArithmeticFactor->identity->name = token->content; token = token->next; - return nullptr; + exit(-1); } else if (token->tokenType == TOKEN_POINTER_OPERATOR) { if (strcmp(token->content, "&") == 0) { //get address for identity @@ -697,7 +781,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { token = token->next; } else { loge(SYNTAX_TAG, "[-]error: except valid identifier or num: %s", token->content); - return nullptr; + exit(-1); } break; } @@ -850,7 +934,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { AstBoolFactorInvert *astBoolFactorInvert = (AstBoolFactorInvert *) currentNode; if (token->tokenType != TOKEN_OPERATOR || strcmp(token->content, "!") != 0) { loge(SYNTAX_TAG, "[-]error: need ;: %s", token->content); - return nullptr; + exit(-1); } //consume ! token = token->next; @@ -868,7 +952,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { if (token->tokenType != TOKEN_OPERATOR && token->tokenType != TOKEN_OPERATOR_2) { loge(SYNTAX_TAG, "[-]error: need relation operator: %s", token->content); - return nullptr; + exit(-1); } if (strcmp(token->content, "==") == 0) { astBoolFactorCompareArithmetic->relationOperation = RELATION_EQ; @@ -884,7 +968,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) { astBoolFactorCompareArithmetic->relationOperation = RELATION_LESS_EQ; } else { loge(SYNTAX_TAG, "[-]error: unknown relation operator: %s", token->content); - return nullptr; + exit(-1); } //consume relation op token = token->next; diff --git a/generator/arm64/assembler_arm64.cpp b/generator/arm64/assembler_arm64.cpp index 50a9d67..b479e57 100644 --- a/generator/arm64/assembler_arm64.cpp +++ b/generator/arm64/assembler_arm64.cpp @@ -3,15 +3,16 @@ // #include -#include +#include +#include #include "assembler_arm64.h" #include "logger.h" #include "file.h" #include "register_arm64.h" #include "binary_arm64.h" #include "linux_syscall.h" - -using namespace std; +#include "mspace.h" +#include "stack.h" #define ARM64_TAG "arm64_asm" @@ -39,8 +40,30 @@ int alignStackSize(int reqSize) { } return blocks * ARM_STACK_ALIGN; } - -static vector currentStackVarList; +// +//struct StackVarListNode { +// StackVar *stackVar; +// StackVarListNode *next; +//}; +//static StackVarListNode *currentStackVarListHead; + +static Stack *currentStackVarStack = createStack(ARM64_TAG); + +static inline void pushStackVar(StackVar *stackVar) { + currentStackVarStack->push(currentStackVarStack, stackVar); +// StackVarListNode *stackVarListNode = (StackVarListNode *) pccMalloc(ARM64_TAG, sizeof(StackVarListNode)); +// stackVarListNode->next = nullptr; +// stackVarListNode->stackVar = stackVar; +// if (currentStackVarListHead == nullptr) { +// currentStackVarListHead = stackVarListNode; +// } else { +// StackVarListNode *p = currentStackVarListHead; +// while (p->next != nullptr) { +// p = p->next; +// } +// p->next = stackVarListNode; +// } +} const char *getSection(SectionType sectionType) { switch (sectionType) { @@ -55,10 +78,13 @@ const char *getSection(SectionType sectionType) { } } -static vector regInUseList; + +static int regInUseList[31]; +static int regInUseTopIndex = 0; static void atomicRegister() { - regInUseList.clear(); + memset(regInUseList, 0, sizeof(int) * 31); + regInUseTopIndex = 0; } char *getCommonRegName(int regIndex, int size) { @@ -179,7 +205,7 @@ int leastRecentlyUseLine(MirCode *mirCode, const char *varName) { int usedRegs = 0; bool isRegAtomicInUse(int bestRegIndex) { - for (int i = 0; i < regInUseList.size(); i++) { + for (int i = 0; i < regInUseTopIndex; i++) { if (regInUseList[i] == bestRegIndex) { return true; } @@ -217,7 +243,7 @@ int allocEmptyReg(MirCode *mirCode) { } } if (bestRegIndex != -1) { - regInUseList.push_back(bestRegIndex); + regInUseList[regInUseTopIndex++] = bestRegIndex; } return bestRegIndex; } @@ -249,16 +275,18 @@ int allocVarFromStack(const char *varName, int sizeInByte) { currentStackTop = alignDownTo(currentStackTop, ARM_BLOCK_64_ALIGN); currentStackTop -= size; stackVar->stackOffset = currentStackTop; - currentStackVarList.push_back(stackVar); + pushStackVar(stackVar); return stackVar->stackOffset; } int getVarSizeFromStack(const char *varName) { - for (int i = 0; i < currentStackVarList.size(); i++) { - if (strcmp(currentStackVarList[i]->varName, varName) == 0) { - return currentStackVarList[i]->varSize; - break; + currentStackVarStack->resetIterator(currentStackVarStack); + StackVar *p = (StackVar *) currentStackVarStack->iteratorNext(currentStackVarStack); + while (p != nullptr) { + if (strcmp(p->varName, varName) == 0) { + return p->varSize; } + p = (StackVar *) currentStackVarStack->iteratorNext(currentStackVarStack); } loge(ARM64_TAG, "unknown var size %s", varName); return -1; @@ -313,15 +341,16 @@ int getOperandSize(MirOperand *mirOperand) { * @param sizeInByte * @return offset from stack bottom */ -int getVarFromStack(const char *varName) { - int offset = -1; - for (int i = 0; i < currentStackVarList.size(); i++) { - if (strcmp(currentStackVarList[i]->varName, varName) == 0) { - offset = currentStackVarList[i]->stackOffset; - break; +uint64_t getVarStackOffset(const char *varName) { + currentStackVarStack->resetIterator(currentStackVarStack); + StackVar *p = (StackVar *) currentStackVarStack->iteratorNext(currentStackVarStack); + while (p != nullptr) { + if (strcmp(p->varName, varName) == 0) { + return p->stackOffset; } + p = (StackVar *) currentStackVarStack->iteratorNext(currentStackVarStack); } - return offset; + return -1; } void releaseStack(int stackSize) { @@ -365,7 +394,7 @@ int loadVarIntoReg(MirCode *mirCode, MirOperand *mirOperand) { } int fromRegIndex = isVarExistInCommonReg(mirOperand->identity); if (fromRegIndex == -1) { - int stackOffset = getVarFromStack(mirOperand->identity); + int stackOffset = getVarStackOffset(mirOperand->identity); if (stackOffset == -1) { loge(ARM64_TAG, "internal error: can not found var %s on stack", mirOperand->identity); return -1; @@ -465,7 +494,7 @@ void generateCodes(MirCode *mirCode) { if (fromValueMirOperand->type.primitiveType == OPERAND_IDENTITY) { int fromRegIndex = isVarExistInCommonReg(fromValueMirOperand->identity); if (fromRegIndex == -1) { - int stackOffset = getVarFromStack(fromValueMirOperand->identity); + int stackOffset = getVarStackOffset(fromValueMirOperand->identity); if (stackOffset == -1) { loge(ARM64_TAG, "internal error: can not found var %s on stack", fromValueMirOperand->identity); } @@ -527,7 +556,7 @@ void generateCodes(MirCode *mirCode) { } } commonRegsVarName[distRegIndex] = mir2->distIdentity; - int distStackOffset = getVarFromStack(mir2->distIdentity); + int distStackOffset = getVarStackOffset(mir2->distIdentity); if (distStackOffset == -1) { int distSize = getMirOperandSizeInByte(mir2->distType); distStackOffset = allocVarFromStack(mir2->distIdentity, distSize); @@ -705,7 +734,7 @@ void generateCodes(MirCode *mirCode) { break; } commonRegsVarName[distRegIndex] = mir3->distIdentity; - int distStackOffset = getVarFromStack(mir3->distIdentity); + int distStackOffset = getVarStackOffset(mir3->distIdentity); if (distStackOffset == -1) { int distSize = getMirOperandSizeInByte(mir3->distType); distStackOffset = allocVarFromStack(mir3->distIdentity, distSize); @@ -980,35 +1009,41 @@ int computeMethodStackSize(MirMethod *mirMethod) { mirMethodParam = mirMethodParam->next; } //compute var size - vector varList; + Stack *currentMethodVarList = createStack(ARM64_TAG);//const char * MirCode *mirCode = mirMethod->code; while (mirCode != nullptr) { switch (mirCode->mirType) { case MIR_3: { bool varFound = false; - for (int i = 0; i < varList.size(); i++) { - if (strcmp(varList[i], mirCode->mir3->distIdentity) == 0) { + currentMethodVarList->resetIterator(currentMethodVarList); + const char *varIdentity = (const char *) currentMethodVarList->iteratorNext(currentMethodVarList); + while (varIdentity != nullptr) { + if (strcmp(varIdentity, mirCode->mir3->distIdentity) == 0) { varFound = true; break; } + varIdentity = (const char *) currentMethodVarList->iteratorNext(currentMethodVarList); } if (!varFound) { stackSizeInByte += ARM_BLOCK_64_ALIGN; - varList.push_back(mirCode->mir3->distIdentity); + currentMethodVarList->push(currentMethodVarList, (void *) mirCode->mir3->distIdentity); } break; } case MIR_2: { bool varFound = false; - for (int i = 0; i < varList.size(); i++) { - if (strcmp(varList[i], mirCode->mir2->distIdentity) == 0) { + currentMethodVarList->resetIterator(currentMethodVarList); + const char *varIdentity = (const char *) currentMethodVarList->iteratorNext(currentMethodVarList); + while (varIdentity != nullptr) { + if (strcmp(varIdentity, mirCode->mir2->distIdentity) == 0) { varFound = true; break; } + varIdentity = (const char *) currentMethodVarList->iteratorNext(currentMethodVarList); } if (!varFound) { stackSizeInByte += ARM_BLOCK_64_ALIGN; - varList.push_back(mirCode->mir2->distIdentity); + currentMethodVarList->push(currentMethodVarList, (void *) mirCode->mir2->distIdentity); } break; } diff --git a/memory/mspace.cpp b/memory/mspace.cpp index 664bcde..53cae3c 100644 --- a/memory/mspace.cpp +++ b/memory/mspace.cpp @@ -7,8 +7,6 @@ #define MSPACE_TAG "mspace" -using namespace std; - struct MemNode { void *addr; MemNode *next; diff --git a/memory/mspace.h b/memory/mspace.h index 4da93a6..090acdc 100644 --- a/memory/mspace.h +++ b/memory/mspace.h @@ -5,6 +5,10 @@ #ifndef PCC_CC_MSPACE_H #define PCC_CC_MSPACE_H +#ifdef __cplusplus +extern "C" { +#endif + #include extern void *pccMalloc(const char *spaceTag, size_t size); @@ -13,4 +17,8 @@ extern void pccFree(const char *spaceTag, void *pointer); extern void pccFreeSpace(const char *spaceTag); +#ifdef __cplusplus +} +#endif + #endif //PCC_CC_MSPACE_H diff --git a/utils/stack.c b/utils/stack.c new file mode 100644 index 0000000..9ef44ec --- /dev/null +++ b/utils/stack.c @@ -0,0 +1,98 @@ +// +// Created by Park Yu on 2024/11/26. +// + +#include "stack.h" +#include "mspace.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static const char *STACK_TAG = "stack"; + +struct StackNode { + void *data; + struct StackNode *next; +}; + +void push(struct Stack *stack, void *data) { + struct StackNode *newNode = (struct StackNode *) pccMalloc(stack->spaceTag, sizeof(struct StackNode)); + newNode->data = data; + newNode->next = NULL; + + if (stack->stackTop != NULL) { + newNode->next = stack->stackTop; + } + stack->stackTop = newNode; + stack->stackSize++; +} + +void *top(struct Stack *stack) { + if (stack->stackTop == NULL) { + return NULL; + } + return stack->stackTop->data; +} + +void *pop(struct Stack *stack) { + if (stack->stackTop == NULL) { + return NULL; + } + struct StackNode *topNode = stack->stackTop; + void *data = topNode->data; + + stack->stackTop = topNode->next; + + pccFree(stack->spaceTag, topNode); + stack->stackSize--; + return data; +} + +int size(struct Stack *stack) { + return stack->stackSize; +} + +void *get(struct Stack *stack, int index) { + if (index < 0 || index >= stack->stackSize) { + return NULL; + } + + struct StackNode *current = stack->stackTop; + for (int i = 0; i < index; i++) { + current = current->next; + } + return current->data; +} + +void resetIterator(struct Stack *stack) { + stack->it = stack->stackTop; +} + +void *iteratorNext(struct Stack *stack) { + if (stack->it == NULL) { + return NULL; + } + void *result = stack->it->data; + stack->it = stack->it->next; + return result; +} + +struct Stack *createStack(const char *spaceTag) { + struct Stack *stack = (struct Stack *) pccMalloc(spaceTag, sizeof(struct Stack)); + stack->spaceTag = spaceTag; + stack->push = push; + stack->top = top; + stack->pop = pop; + stack->size = size; + stack->get = get; + stack->it = NULL; + stack->resetIterator = resetIterator; + stack->iteratorNext = iteratorNext; + stack->stackTop = NULL; + return stack; +} + +#ifdef __cplusplus +} +#endif diff --git a/utils/stack.h b/utils/stack.h new file mode 100644 index 0000000..f3197bc --- /dev/null +++ b/utils/stack.h @@ -0,0 +1,41 @@ +// +// Created by Park Yu on 2024/11/26. +// + +#ifndef PCC_STACK_H +#define PCC_STACK_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct StackNode; + +struct Stack { + const char *spaceTag; + struct StackNode *stackTop; + struct StackNode *it; + int stackSize; + + void (*push)(struct Stack *stack, void *data); + + void *(*top)(struct Stack *stack); + + void *(*pop)(struct Stack *stack); + + int (*size)(struct Stack *stack); + + void *(*get)(struct Stack *stack, int index); + + void (*resetIterator)(struct Stack *stack); + + void *(*iteratorNext)(struct Stack *stack); +}; + +struct Stack *createStack(const char *spaceTag); + +#ifdef __cplusplus +} +#endif + +#endif //PCC_STACK_H