From 742866c8c21259aef74fe78c1e7a12d820d41bdb Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sat, 22 Jan 2022 13:41:24 +0100 Subject: [PATCH] Enable charmap inside a series of string literals --- .gitignore | 1 + oscar64/CompilerTypes.h | 11 ++--- oscar64/GlobalAnalyzer.cpp | 6 ++- oscar64/InterCode.cpp | 2 + oscar64/Parser.cpp | 77 +++++++++++++++++++++++++++-------- oscar64/Parser.h | 1 + samples/memmap/easyflash.crt | Bin 114976 -> 114976 bytes 7 files changed, 76 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index bfdba7c..896dfbf 100644 --- a/.gitignore +++ b/.gitignore @@ -342,3 +342,4 @@ ASALocalRun/ make/oscar64 *.int *.bcs +*.crt diff --git a/oscar64/CompilerTypes.h b/oscar64/CompilerTypes.h index 891d8d0..147d134 100644 --- a/oscar64/CompilerTypes.h +++ b/oscar64/CompilerTypes.h @@ -8,6 +8,7 @@ static const uint64 COPT_OPTIMIZE_INLINE = 0x00000002; static const uint64 COPT_OPTIMIZE_AUTO_INLINE = 0x00000010; static const uint64 COPT_OPTIMIZE_AUTO_INLINE_ALL = 0x00000020; static const uint64 COPT_OPTIMIZE_AUTO_UNROLL = 0x00000040; +static const uint64 COPT_OPTIMIZE_CONST_EXPRESSIONS = 0x00000080; static const uint64 COPT_TARGET_PRG = 0x100000000ULL; static const uint64 COPT_TARGET_CRT16 = 0x200000000ULL; @@ -19,15 +20,15 @@ static const uint64 COPT_VERBOSE = 0x1000000000ULL; static const uint64 COPT_NATIVE = 0x01000000; -static const uint64 COPT_DEFAULT = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE; +static const uint64 COPT_DEFAULT = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE | COPT_OPTIMIZE_CONST_EXPRESSIONS; -static const uint64 COPT_OPTIMIZE_DEFAULT = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE; +static const uint64 COPT_OPTIMIZE_DEFAULT = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE | COPT_OPTIMIZE_CONST_EXPRESSIONS; -static const uint64 COPT_OPTIMIZE_SIZE = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE; +static const uint64 COPT_OPTIMIZE_SIZE = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE | COPT_OPTIMIZE_CONST_EXPRESSIONS; -static const uint64 COPT_OPTIMIZE_SPEED = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE | COPT_OPTIMIZE_AUTO_INLINE | COPT_OPTIMIZE_AUTO_UNROLL; +static const uint64 COPT_OPTIMIZE_SPEED = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE | COPT_OPTIMIZE_AUTO_INLINE | COPT_OPTIMIZE_AUTO_UNROLL | COPT_OPTIMIZE_CONST_EXPRESSIONS; -static const uint64 COPT_OPTIMIZE_ALL = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE | COPT_OPTIMIZE_AUTO_INLINE | COPT_OPTIMIZE_AUTO_INLINE_ALL | COPT_OPTIMIZE_AUTO_UNROLL; +static const uint64 COPT_OPTIMIZE_ALL = COPT_OPTIMIZE_BASIC | COPT_OPTIMIZE_INLINE | COPT_OPTIMIZE_AUTO_INLINE | COPT_OPTIMIZE_AUTO_INLINE_ALL | COPT_OPTIMIZE_AUTO_UNROLL | COPT_OPTIMIZE_CONST_EXPRESSIONS; struct CompilerSettings { diff --git a/oscar64/GlobalAnalyzer.cpp b/oscar64/GlobalAnalyzer.cpp index 329f024..5f48138 100644 --- a/oscar64/GlobalAnalyzer.cpp +++ b/oscar64/GlobalAnalyzer.cpp @@ -163,7 +163,8 @@ void GlobalAnalyzer::AnalyzeProcedure(Expression* exp, Declaration* dec) dec->mFlags |= DTF_FUNC_CONSTEXPR; else if (dec->mFlags & DTF_DEFINED) { - dec->mFlags |= DTF_FUNC_CONSTEXPR; + if (mCompilerOptions & COPT_OPTIMIZE_CONST_EXPRESSIONS) + dec->mFlags |= DTF_FUNC_CONSTEXPR; Analyze(exp, dec); } else @@ -305,7 +306,10 @@ Declaration * GlobalAnalyzer::Analyze(Expression* exp, Declaration* procDec) ldec->mFlags |= DTF_VAR_ALIASING; } else if (exp->mToken == TK_MUL) + { + procDec->mFlags &= ~DTF_FUNC_CONSTEXPR; return exp->mDecType; + } break; case EX_POSTFIX: break; diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index b381c70..8f89749 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -8829,6 +8829,7 @@ void InterCodeProcedure::Disassemble(FILE* file) void InterCodeProcedure::Disassemble(const char* name, bool dumpSets) { +#if 0 #ifdef _WIN32 FILE* file; static bool initial = true; @@ -8864,6 +8865,7 @@ void InterCodeProcedure::Disassemble(const char* name, bool dumpSets) fclose(file); } #endif +#endif } InterCodeModule::InterCodeModule(void) diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index 726acc7..8f03e31 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -615,6 +615,62 @@ Declaration * Parser::CopyConstantInitializer(int offset, Declaration* dtype, Ex return dec; } +uint8* Parser::ParseStringLiteral(int msize) +{ + int size = strlen(mScanner->mTokenString); + if (size + 1 > msize) + msize = size + 1; + uint8* d = new uint8[msize]; + + int i = 0; + while (mScanner->mTokenString[i]) + { + d[i] = mCharMap[mScanner->mTokenString[i]]; + i++; + } + + mScanner->NextToken(); + + while (mScanner->mToken == TK_PREP_PRAGMA) + { + mScanner->NextToken(); + ParsePragma(); + } + + // automatic string concatenation + while (mScanner->mToken == TK_STRING) + { + int s = strlen(mScanner->mTokenString); + + if (size + s + 1 > msize) + msize = size + s + 1; + + uint8* nd = new uint8[msize]; + memcpy(nd, d, size); + int i = 0; + while (mScanner->mTokenString[i]) + { + nd[i + size] = mCharMap[mScanner->mTokenString[i]]; + i++; + } + size += s; + delete[] d; + d = nd; + mScanner->NextToken(); + + while (mScanner->mToken == TK_PREP_PRAGMA) + { + mScanner->NextToken(); + ParsePragma(); + } + } + + while (size < msize) + d[size++] = 0; + + return d; +} + Expression* Parser::ParseInitExpression(Declaration* dtype) { Expression* exp = nullptr; @@ -713,10 +769,13 @@ Expression* Parser::ParseInitExpression(Declaration* dtype) } else if (mScanner->mToken == TK_STRING && dtype->mType == DT_TYPE_ARRAY && dtype->mBase->mType == DT_TYPE_INTEGER && dtype->mBase->mSize == 1) { + uint8* d = ParseStringLiteral(dtype->mSize); + int ds = strlen((char *)d); + if (!(dtype->mFlags & DTF_DEFINED)) { dtype->mFlags |= DTF_DEFINED; - dtype->mSize = strlen(mScanner->mTokenString) + 1; + dtype->mSize = ds + 1; } dec = new Declaration(mScanner->mLocation, DT_CONST_DATA); @@ -724,24 +783,10 @@ Expression* Parser::ParseInitExpression(Declaration* dtype) dec->mSize = dtype->mSize; dec->mSection = mDataSection; - uint8* d = new uint8[dtype->mSize]; dec->mData = d; - if (strlen(mScanner->mTokenString) < dtype->mSize) - { - int i = 0; - while (i < dec->mSize && mScanner->mTokenString[i]) - { - d[i] = mCharMap[mScanner->mTokenString[i]]; - i++; - } - if (i < dec->mSize) - d[i] = 0; - } - else + if (ds > dtype->mSize) mErrors->Error(mScanner->mLocation, EERR_CONSTANT_INITIALIZER, "String constant is too large for char array"); - - mScanner->NextToken(); } else { diff --git a/oscar64/Parser.h b/oscar64/Parser.h index bc3640e..8c782ed 100644 --- a/oscar64/Parser.h +++ b/oscar64/Parser.h @@ -23,6 +23,7 @@ protected: char mCharMap[256]; + uint8* ParseStringLiteral(int msize); void ParsePragma(void); diff --git a/samples/memmap/easyflash.crt b/samples/memmap/easyflash.crt index 9d6299c4bfad8b97ebb10c7c7b963ddab81e7902..133dcc5ffaf5a2fd2f11f99aac6564a72ba39a9d 100644 GIT binary patch delta 663 zcma*jKWI}?6bA77=hgJ4H+hXMB`*be`ZAbBN*yc}NhM8c!xJ*O*rAJy&_&SE*2IHS zxX{-tI2y4LkKz3XTb@%Xbj##YsgjBnw|6+x;m3F2Yt|FZdSa_F-vI|D zzL_6$`1PjaC#Uts-}!T(-R^6dl^RiJrOeV z0&ky^p7`_l#P{@N=AEfaV0#U{!dB?M-enKyLp{TD?EIMa`7Qcgzv6jOm)Scuu-%3> zyl8)rJEh=8Ya&=YDEX68pR+E}d+nRn^XYcYopmq#YX=v)frlHzo}P`J7I;mzcNUI| baom@Ucy9l5Ecel(G#<=fj|mqa(SGqa6!`+} delta 560 zcma*hFGvGX90&0C?%t+zZ#(B?f6L)b(4fJhh_K1Vm}B9RK@1j)m<(cbDnAh6fyWCL z!GRO@6HG&NK`Fjqw5Mk|-WimvAg6Y0|;|IRq#^F%oaOm`C(S$T|$Sff5aZkHO z(jZp4YxmzX&-TXxuJufBxP|1{O51%LfPwpv(@Cfn;xZPH(xiMQ4dXZkaU?JXF`NgA z`y%3qh%1PL!y@Vk%@I*oXs$@u(xb@@%!-(e3`$5qLk6Qa#GU?xGP3GVR@6XQ5En_g zwDqsk9uzTEtp0LTEM=%Rt_>3_UL|=^TBP7m5)~a8MqSva77qJvpiF z(lL3iW|Z3zd9SY09jUQgD8W;k8YrElTw-~x!ka~_auv5q@5NdxEKk@h4YoV%ENj`@ yZBw7qC;qmu(6tP#f3Hr3fA746+s(ot@5epW{kgYCU+#`k>JR3YeZtZKb@E>ntJBi}