Implemented static const int/float/bool etc constat propagation

This commit is contained in:
drmortalwombat 2021-09-06 21:13:48 +02:00
parent 95b1608301
commit 60354f5e2d
6 changed files with 52 additions and 4 deletions

View File

@ -47,7 +47,6 @@ The first release of the compiler is severely limited considering it is only two
* All global variables are considered volatile * All global variables are considered volatile
* No loop opmtimization * No loop opmtimization
* Static const not propagated
* Poor bookeeping of callee saved registers * Poor bookeeping of callee saved registers
* Missing livetime reduction of intermediates * Missing livetime reduction of intermediates
* No block domination analysis * No block domination analysis

View File

@ -27,6 +27,9 @@ if %errorlevel% neq 0 goto :error
..\release\oscar64 -i=../include -rt=../include/crt.c -e floatmultest.c ..\release\oscar64 -i=../include -rt=../include/crt.c -e floatmultest.c
if %errorlevel% neq 0 goto :error 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 exit /b 0
:error :error
echo Failed with error #%errorlevel%. echo Failed with error #%errorlevel%.

View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <assert.h>
static const int size = 100;
int main(void)
{
int a[size];
for(int i=0; i<size; i++)
a[i] = i;
int s = 0;
for(int i=0; i<size; i++)
s += a[i];
assert(s == 4950);
return 0;
}

View File

@ -44,6 +44,14 @@ Declaration* Parser::ParseBaseTypeDeclaration(uint32 flags)
break; break;
case TK_STATIC:
mScanner->NextToken();
return ParseBaseTypeDeclaration(flags | DTF_STATIC);
case TK_EXTERN:
mScanner->NextToken();
return ParseBaseTypeDeclaration(flags | DTF_EXTERN);
case TK_CONST: case TK_CONST:
mScanner->NextToken(); mScanner->NextToken();
return ParseBaseTypeDeclaration(flags | DTF_CONST); return ParseBaseTypeDeclaration(flags | DTF_CONST);
@ -638,6 +646,8 @@ Declaration* Parser::ParseDeclaration(bool variable)
{ {
if (variable) if (variable)
{ {
ndec->mFlags |= ndec->mBase->mFlags & (DTF_CONST | DTF_STATIC | DTF_VOLATILE | DTF_EXTERN);
if (ndec->mBase->mType == DT_TYPE_FUNCTION) if (ndec->mBase->mType == DT_TYPE_FUNCTION)
ndec->mType = DT_CONST_FUNCTION; ndec->mType = DT_CONST_FUNCTION;
@ -783,6 +793,7 @@ Expression* Parser::ParseSimpleExpression(void)
case TK_VOLATILE: case TK_VOLATILE:
case TK_STRUCT: case TK_STRUCT:
case TK_TYPEDEF: case TK_TYPEDEF:
case TK_STATIC:
exp = ParseDeclarationExpression(); exp = ParseDeclarationExpression();
break; break;
case TK_INTEGER: case TK_INTEGER:
@ -885,9 +896,16 @@ Expression* Parser::ParseSimpleExpression(void)
} }
else if (dec->mType == DT_VARIABLE || dec->mType == DT_ARGUMENT) else if (dec->mType == DT_VARIABLE || dec->mType == DT_ARGUMENT)
{ {
exp = new Expression(mScanner->mLocation, EX_VARIABLE); if ((dec->mFlags & DTF_STATIC) && (dec->mFlags & DTF_CONST) && dec->mValue && dec->mBase->IsNumericType())
exp->mDecValue = dec; {
exp->mDecType = dec->mBase; exp = dec->mValue;
}
else
{
exp = new Expression(mScanner->mLocation, EX_VARIABLE);
exp->mDecValue = dec;
exp->mDecType = dec->mBase;
}
mScanner->NextToken(); mScanner->NextToken();
} }
else if (dec->mType <= DT_TYPE_FUNCTION) else if (dec->mType <= DT_TYPE_FUNCTION)

View File

@ -38,6 +38,8 @@ const char* TokenNames[] = {
"'union'", "'union'",
"'enum'", "'enum'",
"'sizeof'", "'sizeof'",
"'static'",
"'extern'",
"__asm", "__asm",
@ -958,6 +960,10 @@ void Scanner::NextRawToken(void)
mToken = TK_SIZEOF; mToken = TK_SIZEOF;
else if (!strcmp(tkident, "typedef")) else if (!strcmp(tkident, "typedef"))
mToken = TK_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")) else if (!strcmp(tkident, "__asm"))
mToken = TK_ASM; mToken = TK_ASM;
else else

View File

@ -38,6 +38,8 @@ enum Token
TK_UNION, TK_UNION,
TK_ENUM, TK_ENUM,
TK_SIZEOF, TK_SIZEOF,
TK_STATIC,
TK_EXTERN,
TK_ASM, TK_ASM,