Skip to content

Commit

Permalink
update: milestone! hello world stdout!
Browse files Browse the repository at this point in the history
  • Loading branch information
park671 committed Nov 25, 2024
1 parent 2ec474e commit 5d677ea
Show file tree
Hide file tree
Showing 20 changed files with 329 additions and 72 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,11 @@ add_executable(pcc
generator/arm64/internal_func_arm64.cpp
generator/arm64/internal_func_arm64.h
)

add_custom_command(
TARGET pcc POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
COMMENT "Copying include directory to build output"
)
8 changes: 7 additions & 1 deletion compiler/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,19 @@ struct AstStatementBlock {
//}
};

enum MethodDefineType {
METHOD_IMPL,
METHOD_EXTERN,
};

struct AstMethodDefine {
MethodDefineType defineType;
AstType *type;
AstIdentity *identity;
// (
AstParamList *paramList;
//)
AstStatementBlock *statementBlock;
AstStatementBlock *statementBlock;//nullable
};

struct AstMethodSeq {
Expand Down
44 changes: 25 additions & 19 deletions compiler/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@

using namespace std;

//16KB
#define BUFFER_SIZE 16384

static const char *LEXER_TAG = "lexer";
static const char *VAR_TAG = "var";

static const char *keywords[] = {"if", "else", "for", "while", "return"};
static const size_t keywordSize = 5;
static const char *keywords[] = {"if", "else", "for", "while", "return", "extern"};
static const size_t keywordSize = 6;

static const char *types[] = {"void", "char", "int", "short", "long", "float", "double"};
static const size_t typeSize = 7;
Expand Down Expand Up @@ -60,26 +57,26 @@ const char *getTokenTypeName(TokenType tokenType) {
return "unknown";
}

static void trimString(char *buffer) {
static void trimString(ProcessedSource *source) {
int startIndex = -1;
int endIndex = -1;
for (int i = 0; i < BUFFER_SIZE; i++) {
for (int i = 0; i < source->length; i++) {
if (startIndex == -1) {
if (buffer[i] == ' ') {
if (source->line[i] == ' ') {
continue;
}
startIndex = i;
}
if (buffer[i] == '\n' || buffer[i] == '\0') {
if (source->line[i] == '\n' || source->line[i] == '\0') {
endIndex = i;
break;
}
}
int size = endIndex - startIndex;
for (int i = 0; i < size; i++) {
buffer[i] = buffer[i + startIndex];
source->line[i] = source->line[i + startIndex];
}
buffer[size] = '\0';
source->line[size] = '\0';
}

inline static bool isBoolOperator(char a) {
Expand Down Expand Up @@ -186,10 +183,21 @@ static Token *lexer(Token *tail, const char *buffer) {
bool nextCharEscape = false;
bool inCharsSession = false;
for (int i = 0; i < length; i++) {

if (inCharsSession && isEscape(buffer[i])) {
nextCharEscape = true;
continue;
} else if (nextCharEscape) {
if (buffer[i] == 'n') {
snprintf(tmp, 256, "%s\n", tmp);
} else if (buffer[i] == 'r') {
snprintf(tmp, 256, "%s\r", tmp);
} else if (buffer[i] == 't') {
snprintf(tmp, 256, "%s\t", tmp);
} else if (buffer[i] == 'b') {
snprintf(tmp, 256, "%s\b", tmp);
}
nextCharEscape = false;
continue;
} else if (isQuotation(buffer[i]) && !nextCharEscape) {
if (!inCharsSession) {
tail = processCompletedString(tail, tmp);
Expand Down Expand Up @@ -293,18 +301,16 @@ static Token *lexer(Token *tail, const char *buffer) {
return tail;
}

Token *buildTokens(const char *sourceFilePath) {
Token *buildTokens(ProcessedSource *source) {
logd(LEXER_TAG, "lexical analysis...");
FILE *inputFile = fopen(sourceFilePath, "r");
char buffer[BUFFER_SIZE];
Token *tokenHead = (Token *) (pccMalloc(LEXER_TAG, sizeof(Token)));
tokenHead->tokenType = TOKEN_HEAD;
Token *tokenTail = tokenHead;
while (fgets(buffer, BUFFER_SIZE, inputFile) != nullptr) {
trimString(buffer);
tokenTail = lexer(tokenTail, buffer);
while (source != nullptr) {
trimString(source);
tokenTail = lexer(tokenTail, source->line);
source = source->next;
}
fclose(inputFile);
return tokenHead;
}

Expand Down
5 changes: 4 additions & 1 deletion compiler/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
#define PCC_CC_LEXER_H

#include "token.h"
#include "preprocessor.h"

extern Token *buildTokens(ProcessedSource *source);

extern Token *buildTokens(const char *sourceFilePath);
extern void printTokenStack(Token *tokens);

extern void releaseLexerMemory();

#endif //PCC_CC_LEXER_H
34 changes: 24 additions & 10 deletions compiler/mir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,9 @@ void generateParam(AstParamList *astParamList, MirMethod *mirMethod) {
//real travel ast
mirMethodParam->paramName = curAstParamList->paramDefine->identity->name;
mirMethodParam->sign = true;
if (curAstParamList->paramDefine->type->isPointer) {
mirMethodParam->pointer = true;
}
switch (curAstParamList->paramDefine->type->primitiveType) {
case TYPE_CHAR: {
mirMethodParam->integer = true;
Expand Down Expand Up @@ -1368,6 +1371,13 @@ void generateMethod(AstMethodDefine *astMethodDefine, MirMethod *mirMethod) {
addMethodInfo(
astMethodDefine->identity->name,
type);
if (astMethodDefine->defineType == METHOD_EXTERN) {
logd(MIR_TAG, "skip extern method mir generation: %s", astMethodDefine->identity->name);
mirMethod->isExtern = true;
resetTempValIndex();
return;
}
mirMethod->isExtern = false;
AstStatementSeq *astStatementSeq = astMethodDefine->statementBlock->statementSeq;
//start mir code session
startMirCodeSession();
Expand Down Expand Up @@ -1396,17 +1406,21 @@ Mir *generateMir(AstProgram *program) {
while (astMethodSeq != nullptr) {
//maintain mir linked-list
mirMethod = (MirMethod *) pccMalloc(MIR_TAG, sizeof(MirMethod));
mirMethod->next = nullptr;
if (firstMirMethod == nullptr) {
firstMirMethod = mirMethod;
}
if (lastMirMethod != nullptr) {
lastMirMethod->next = mirMethod;
}
lastMirMethod = mirMethod;
//real travel ast
methodSize++;
generateMethod(astMethodSeq->methodDefine, mirMethod);
if (mirMethod->isExtern) {
pccFree(MIR_TAG, mirMethod);
} else {
mirMethod->next = nullptr;
if (firstMirMethod == nullptr) {
firstMirMethod = mirMethod;
}
if (lastMirMethod != nullptr) {
lastMirMethod->next = mirMethod;
}
lastMirMethod = mirMethod;
//real travel ast
methodSize++;
}
astMethodSeq = astMethodSeq->nextAstMethodSeq;
}
mir->mirMethod = firstMirMethod;
Expand Down
2 changes: 2 additions & 0 deletions compiler/mir.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum MirType {
struct MirCode;

struct MirMethod {
bool isExtern = false;
const char *label;//identity, in another word: method name.
MirMethodParam *param;//nullable
MirCode *code;
Expand All @@ -45,6 +46,7 @@ struct MirMethod {

struct MirMethodParam {
const char *paramName;
bool pointer;
bool integer;//true-int, false-float
bool sign;//true-signed, false-unsigned
int byte;//1-int8, 2-int16, 4-int32, 8-int64
Expand Down
17 changes: 12 additions & 5 deletions compiler/optimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ bool replaceFutureUsedMir2(MirCode *currentMirCode, const char *identity, MirOpe

if (mirCode->mirType == MIR_2) {
Mir2 *mir2 = mirCode->mir2;
if (mir2->fromValue.type.primitiveType == OPERAND_IDENTITY && strcmp(mir2->fromValue.identity, identity) == 0) {
if (mir2->fromValue.type.primitiveType == OPERAND_IDENTITY &&
strcmp(mir2->fromValue.identity, identity) == 0) {
mir2->fromValue = *replaceWith;
result = true;
}
Expand All @@ -81,18 +82,21 @@ bool replaceFutureUsedMir2(MirCode *currentMirCode, const char *identity, MirOpe
} else if (mirCode->mirType == MIR_RET) {
MirRet *mirRet = mirCode->mirRet;
if (mirRet->value != nullptr) {
if (mirRet->value->type.primitiveType == OPERAND_IDENTITY && strcmp(mirRet->value->identity, identity) == 0) {
if (mirRet->value->type.primitiveType == OPERAND_IDENTITY &&
strcmp(mirRet->value->identity, identity) == 0) {
mirRet->value = replaceWith;
result = true;
}
}
} else if (mirCode->mirType == MIR_CMP) {
MirCmp *mirCmp = mirCode->mirCmp;
if (mirCmp->value1.type.primitiveType == OPERAND_IDENTITY && strcmp(mirCmp->value1.identity, identity) == 0) {
if (mirCmp->value1.type.primitiveType == OPERAND_IDENTITY &&
strcmp(mirCmp->value1.identity, identity) == 0) {
mirCmp->value1 = *replaceWith;
result = true;
}
if (mirCmp->value2.type.primitiveType == OPERAND_IDENTITY && strcmp(mirCmp->value2.identity, identity) == 0) {
if (mirCmp->value2.type.primitiveType == OPERAND_IDENTITY &&
strcmp(mirCmp->value2.identity, identity) == 0) {
mirCmp->value2 = *replaceWith;
result = true;
}
Expand Down Expand Up @@ -144,7 +148,10 @@ void printMirCode(MirMethod *mirMethod);
void foldMir2(MirMethod *mirMethod) {
MirCode *mirCode = mirMethod->code;
while (mirCode != nullptr) {
if (mirCode->mirType == MIR_2) {
if (mirCode->mirType == MIR_2
&& mirCode->mir2->op == OP_ASSIGNMENT
&& !mirCode->mir2->fromValue.type.isPointer) {
//todo this is a BIG shit, will fix in future
Mir2 *mir2 = mirCode->mir2;
if (replaceFutureUsedMir2(mirCode, mir2->distIdentity, &mir2->fromValue)) {
mirMethod->code = removeMirCode(mirMethod, mirCode);
Expand Down
Loading

0 comments on commit 5d677ea

Please sign in to comment.