Skip to content

Commit eae2d4c

Browse files
committed
Re-enable macro caching.
1 parent 8e915f7 commit eae2d4c

File tree

6 files changed

+95
-50
lines changed

6 files changed

+95
-50
lines changed

source/base/fileinputoutput.cpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -511,37 +511,35 @@ POV_OFF_T IMemStream::tellg() const
511511

512512
bool IMemStream::seekg(POV_OFF_T posi, unsigned int whence)
513513
{
514-
if(!fail)
515-
{
516-
// Any seek operation renders the unget buffer's content obsolete.
517-
mUngetBuffer = EOF;
514+
// Any seek operation renders the end-of-file status and unget buffer's content obsolete.
515+
fail = false;
516+
mUngetBuffer = EOF;
518517

519-
switch(whence)
520-
{
521-
case seek_set:
522-
if (posi < formalStart)
523-
fail = true;
524-
else if (posi - formalStart <= size)
525-
pos = posi - formalStart;
526-
else
527-
fail = true;
528-
break;
529-
case seek_cur:
530-
if ((posi <= size) && (pos <= size-posi))
531-
pos += posi;
532-
else
533-
fail = true;
534-
break;
535-
case seek_end:
536-
if (posi <= size)
537-
pos = size - posi;
538-
else
539-
fail = true;
540-
break;
541-
default:
542-
POV_ASSERT(false);
543-
break;
544-
}
518+
switch(whence)
519+
{
520+
case seek_set:
521+
if (posi < formalStart)
522+
fail = true;
523+
else if (posi - formalStart <= size)
524+
pos = posi - formalStart;
525+
else
526+
fail = true;
527+
break;
528+
case seek_cur:
529+
if ((posi <= size) && (pos <= size-posi))
530+
pos += posi;
531+
else
532+
fail = true;
533+
break;
534+
case seek_end:
535+
if (posi <= size)
536+
pos = size - posi;
537+
else
538+
fail = true;
539+
break;
540+
default:
541+
POV_ASSERT(false);
542+
break;
545543
}
546544
return !fail;
547545
}

source/parser/parser_tokenizer.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,24 +1321,19 @@ void Parser::Parse_Directive(int After_Hash)
13211321
PMac->endPosition = hashPosition;
13221322
POV_OFF_T macroLength = mToken.raw.lexeme.position - PMac->source;
13231323
/// @todo Re-enable cached macros.
1324-
#if 0
13251324
if (macroLength <= MaxCachedMacroSize)
13261325
{
13271326
PMac->CacheSize = macroLength;
13281327
PMac->Cache = new unsigned char[PMac->CacheSize];
1329-
if (PMac->Cache)
1328+
RawTokenizer::HotBookmark pos = mTokenizer.GetHotBookmark();
1329+
mTokenizer.GoToBookmark(PMac->source);
1330+
if (!mTokenizer.GetRaw(PMac->Cache, PMac->CacheSize))
13301331
{
1331-
mTokenizer.GoToBookmark(PMac->source);
1332-
Input_File->inFile->seekg(PMac->Macro_File_Pos);
1333-
if (!Input_File->inFile->ReadRaw(PMac->Cache, PMac->CacheSize))
1334-
{
1335-
delete[] PMac->Cache;
1336-
PMac->Cache = nullptr;
1337-
}
1338-
Input_File->inFile->seekg(pos);
1332+
delete[] PMac->Cache;
1333+
PMac->Cache = nullptr;
13391334
}
1335+
mTokenizer.GoToBookmark(pos);
13401336
}
1341-
#endif
13421337
}
13431338
}
13441339
Cond_Stack.pop_back();
@@ -2580,7 +2575,6 @@ void Parser::Invoke_Macro()
25802575
UCS2String ign;
25812576
/* Not in same file */
25822577
Cond_Stack.back().Macro_Same_Flag = false;
2583-
Cond_Stack.back().returnToBookmark = mTokenizer.GetHotBookmark();
25842578
Got_EOF=false;
25852579
POV_PARSER_ASSERT(!readingExternalFile);
25862580
shared_ptr<IStream> is;
@@ -2604,7 +2598,8 @@ void Parser::Invoke_Macro()
26042598
Got_EOF=false;
26052599
if (!mTokenizer.GoToBookmark(PMac->source))
26062600
{
2607-
Error("Unable to file seek in macro.");
2601+
ErrorInfo(mToken, "Invoking macro from here.");
2602+
Error(PMac->source, "Unable to file seek in macro invocation.");
26082603
}
26092604

26102605
mToken.sourceFile = mTokenizer.GetSource();
@@ -2621,17 +2616,16 @@ void Parser::Return_From_Macro()
26212616
{
26222617
Check_Macro_Vers();
26232618

2624-
if (!Cond_Stack.back().Macro_Same_Flag)
2625-
{
2626-
POV_PARSER_ASSERT(!readingExternalFile);
2627-
mTokenizer.SetInputStream(Cond_Stack.back().returnToBookmark.pSource);
2628-
mToken.sourceFile = mTokenizer.GetSource();
2629-
}
2619+
POV_PARSER_ASSERT(!readingExternalFile);
26302620

26312621
Got_EOF=false;
26322622

26332623
if (!mTokenizer.GoToBookmark(Cond_Stack.back().returnToBookmark))
2634-
Error("Unable to file seek in return from macro.");
2624+
{
2625+
ErrorInfo(mToken, "Returning from macro.");
2626+
Error(Cond_Stack.back().returnToBookmark, "Unable to file seek in return from macro.");
2627+
}
2628+
mToken.sourceFile = mTokenizer.GetSource();
26352629

26362630
// Always destroy macro locals
26372631
Destroy_Table(Table_Index--);

source/parser/rawtokenizer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ TokenId pov_parser::RawTokenizer::GetExpressionId(TokenId tokenId)
369369

370370
//------------------------------------------------------------------------------
371371

372+
bool RawTokenizer::GetRaw(unsigned char* buffer, size_t size)
373+
{
374+
return mScanner.GetRaw(buffer, size);
375+
}
376+
377+
//------------------------------------------------------------------------------
378+
372379
pov_parser::ConstSourcePtr RawTokenizer::GetSource() const
373380
{
374381
return mScanner.GetSource();

source/parser/rawtokenizer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ class RawTokenizer
169169
/// Advance to the next `#` token in the input stream.
170170
bool GetNextDirective(RawToken& token);
171171

172+
/// Read raw data.
173+
/// @deprecated
174+
/// This method is only intended as a temporary measure to implement
175+
/// binary-level macro caching, to provide a reference for performance
176+
/// testing of the envisioned token-level macro caching.
177+
bool GetRaw(unsigned char* buffer, size_t size);
178+
172179
/// Get current source for comparison.
173180
ConstSourcePtr GetSource() const;
174181

source/parser/scanner.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,38 @@ bool Scanner::EatNextBlockComment()
744744

745745
//------------------------------------------------------------------------------
746746

747+
bool Scanner::GetRaw(unsigned char* buffer, size_t size)
748+
{
749+
POV_PARSER_ASSERT(!mEndOfStream);
750+
751+
unsigned char* pBufPos = buffer;
752+
size_t sizeToCopy = size;
753+
size_t sizeInBuffer = (mpBufferEnd - mpNextChar);
754+
while (!mEndOfStream && (sizeToCopy >= sizeInBuffer))
755+
{
756+
memcpy(pBufPos, mpNextChar, sizeInBuffer);
757+
pBufPos += sizeInBuffer;
758+
mpNextChar += sizeInBuffer;
759+
POV_PARSER_ASSERT(mpNextChar == mpBufferEnd);
760+
RefillBuffer();
761+
sizeToCopy -= sizeInBuffer;
762+
sizeInBuffer = (mpBufferEnd - mpNextChar);
763+
}
764+
765+
if (!mEndOfStream && (sizeToCopy > 0))
766+
{
767+
POV_PARSER_ASSERT(sizeToCopy < sizeInBuffer);
768+
memcpy(pBufPos, mpNextChar, sizeToCopy);
769+
pBufPos += sizeToCopy;
770+
mpNextChar += sizeToCopy;
771+
sizeToCopy -= sizeToCopy;
772+
}
773+
774+
return (sizeToCopy == 0);
775+
}
776+
777+
//------------------------------------------------------------------------------
778+
747779
ConstSourcePtr Scanner::GetSource() const
748780
{
749781
return mpSource;

source/parser/scanner.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ class Scanner
159159
/// Advance to the next `#` lexeme in the input stream.
160160
bool GetNextDirective(Lexeme& lexeme);
161161

162+
/// Read raw data.
163+
/// @deprecated
164+
/// This method is only intended as a temporary measure to implement
165+
/// binary-level macro caching, to provide a reference for performance
166+
/// testing of the envisioned token-level macro caching.
167+
bool GetRaw(unsigned char* buffer, size_t size);
168+
162169
/// Get current source for comparison.
163170
ConstSourcePtr GetSource() const;
164171

0 commit comments

Comments
 (0)