From 60354f5e2ddcfcc8493e001052ba93641a786036 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Mon, 6 Sep 2021 21:13:48 +0200 Subject: [PATCH] Implemented static const int/float/bool etc constat propagation --- README.md | 1 - autotest/autotest.bat | 3 +++ autotest/staticconsttest.c | 20 ++++++++++++++++++++ oscar64/Parser.cpp | 24 +++++++++++++++++++++--- oscar64/Scanner.cpp | 6 ++++++ oscar64/Scanner.h | 2 ++ 6 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 autotest/staticconsttest.c diff --git a/README.md b/README.md index 222bb7e..48f6827 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,6 @@ The first release of the compiler is severely limited considering it is only two * All global variables are considered volatile * No loop opmtimization -* Static const not propagated * Poor bookeeping of callee saved registers * Missing livetime reduction of intermediates * No block domination analysis diff --git a/autotest/autotest.bat b/autotest/autotest.bat index 1d2adc2..afc379c 100644 --- a/autotest/autotest.bat +++ b/autotest/autotest.bat @@ -27,6 +27,9 @@ if %errorlevel% neq 0 goto :error ..\release\oscar64 -i=../include -rt=../include/crt.c -e floatmultest.c if %errorlevel% neq 0 goto :error +..\release\oscar64 -i=../include -rt=../include/crt.c -e staticconsttest.c +if %errorlevel% neq 0 goto :error + exit /b 0 :error echo Failed with error #%errorlevel%. diff --git a/autotest/staticconsttest.c b/autotest/staticconsttest.c new file mode 100644 index 0000000..1c703f2 --- /dev/null +++ b/autotest/staticconsttest.c @@ -0,0 +1,20 @@ +#include +#include + +static const int size = 100; + +int main(void) +{ + int a[size]; + + for(int i=0; iNextToken(); + return ParseBaseTypeDeclaration(flags | DTF_STATIC); + + case TK_EXTERN: + mScanner->NextToken(); + return ParseBaseTypeDeclaration(flags | DTF_EXTERN); + case TK_CONST: mScanner->NextToken(); return ParseBaseTypeDeclaration(flags | DTF_CONST); @@ -638,6 +646,8 @@ Declaration* Parser::ParseDeclaration(bool variable) { if (variable) { + ndec->mFlags |= ndec->mBase->mFlags & (DTF_CONST | DTF_STATIC | DTF_VOLATILE | DTF_EXTERN); + if (ndec->mBase->mType == DT_TYPE_FUNCTION) ndec->mType = DT_CONST_FUNCTION; @@ -783,6 +793,7 @@ Expression* Parser::ParseSimpleExpression(void) case TK_VOLATILE: case TK_STRUCT: case TK_TYPEDEF: + case TK_STATIC: exp = ParseDeclarationExpression(); break; case TK_INTEGER: @@ -885,9 +896,16 @@ Expression* Parser::ParseSimpleExpression(void) } else if (dec->mType == DT_VARIABLE || dec->mType == DT_ARGUMENT) { - exp = new Expression(mScanner->mLocation, EX_VARIABLE); - exp->mDecValue = dec; - exp->mDecType = dec->mBase; + if ((dec->mFlags & DTF_STATIC) && (dec->mFlags & DTF_CONST) && dec->mValue && dec->mBase->IsNumericType()) + { + exp = dec->mValue; + } + else + { + exp = new Expression(mScanner->mLocation, EX_VARIABLE); + exp->mDecValue = dec; + exp->mDecType = dec->mBase; + } mScanner->NextToken(); } else if (dec->mType <= DT_TYPE_FUNCTION) diff --git a/oscar64/Scanner.cpp b/oscar64/Scanner.cpp index 463297d..3d3e076 100644 --- a/oscar64/Scanner.cpp +++ b/oscar64/Scanner.cpp @@ -38,6 +38,8 @@ const char* TokenNames[] = { "'union'", "'enum'", "'sizeof'", + "'static'", + "'extern'", "__asm", @@ -958,6 +960,10 @@ void Scanner::NextRawToken(void) mToken = TK_SIZEOF; else if (!strcmp(tkident, "typedef")) mToken = TK_TYPEDEF; + else if (!strcmp(tkident, "static")) + mToken = TK_STATIC; + else if (!strcmp(tkident, "extern")) + mToken = TK_EXTERN; else if (!strcmp(tkident, "__asm")) mToken = TK_ASM; else diff --git a/oscar64/Scanner.h b/oscar64/Scanner.h index 1096749..dacb53b 100644 --- a/oscar64/Scanner.h +++ b/oscar64/Scanner.h @@ -38,6 +38,8 @@ enum Token TK_UNION, TK_ENUM, TK_SIZEOF, + TK_STATIC, + TK_EXTERN, TK_ASM,