From 2596af84faf82b0cd93c56989bd774789be03d66 Mon Sep 17 00:00:00 2001 From: Rika Ichinose Date: Wed, 22 Mar 2023 13:37:47 +0300 Subject: [PATCH] Optimize {$DEFINE x := y} and refactor tmacro.buftext. --- compiler/scanner.pas | 44 +++++++++---------------------------------- compiler/symsym.pas | 36 ++++++++++++++++++++++++----------- compiler/symtable.pas | 23 +++++----------------- 3 files changed, 39 insertions(+), 64 deletions(-) diff --git a/compiler/scanner.pas b/compiler/scanner.pas index 153bfad3cd..686dc61fd1 100644 --- a/compiler/scanner.pas +++ b/compiler/scanner.pas @@ -2544,8 +2544,8 @@ type hs : string; bracketcount : longint; mac : tmacro; - macropos : longint; - macrobuffer : pmacrobuffer; + macropos : SizeInt; + macrobuffer : array[0..maxmacrolen-1] of char; begin current_scanner.skipspace; hs:=current_scanner.readid; @@ -2566,11 +2566,7 @@ type mac.defined:=true; mac.is_compiler_var:=false; { delete old definition } - if assigned(mac.buftext) then - begin - freemem(mac.buftext,mac.buflen); - mac.buftext:=nil; - end; + mac.free_buftext; end; Message1(parser_c_macro_defined,mac.name); mac.is_used:=true; @@ -2594,7 +2590,6 @@ type if is_keyword(hs) then Message(scan_e_keyword_cant_be_a_macro); - new(macrobuffer); macropos:=0; { parse macro, brackets are counted so it's possible to have a $ifdef etc. in the macro } @@ -2613,22 +2608,15 @@ type #26 : current_scanner.end_of_file; end; - macrobuffer^[macropos]:=c; - inc(macropos); if macropos>=maxmacrolen then Message(scan_f_macro_buffer_overflow); + macrobuffer[macropos]:=c; + inc(macropos); current_scanner.readchar; until false; - { free buffer of macro ?} - if assigned(mac.buftext) then - freemem(mac.buftext,mac.buflen); - { get new mem } - getmem(mac.buftext,macropos); - mac.buflen:=macropos; { copy the text } - move(macrobuffer^,mac.buftext^,macropos); - dispose(macrobuffer); + move(pchar(@macrobuffer[0])^,mac.allocate_buftext(macropos)^,macropos); end else begin @@ -2676,11 +2664,7 @@ type mac.defined:=true; mac.is_compiler_var:=true; { delete old definition } - if assigned(mac.buftext) then - begin - freemem(mac.buftext,mac.buflen); - mac.buftext:=nil; - end; + mac.free_buftext; end; Message1(parser_c_macro_defined,mac.name); mac.is_used:=true; @@ -2714,14 +2698,8 @@ type hs:='FALSE'; end; Message2(parser_c_macro_set_to,mac.name,hs); - { free buffer of macro ?} - if assigned(mac.buftext) then - freemem(mac.buftext,mac.buflen); - { get new mem } - getmem(mac.buftext,length(hs)); - mac.buflen:=length(hs); { copy the text } - move(hs[1],mac.buftext^,mac.buflen); + move(hs[1],mac.allocate_buftext(length(hs))^,length(hs)); end else Message(scan_e_preproc_syntax_error); @@ -2752,11 +2730,7 @@ type mac.defined:=false; mac.is_compiler_var:=false; { delete old definition } - if assigned(mac.buftext) then - begin - freemem(mac.buftext,mac.buflen); - mac.buftext:=nil; - end; + mac.free_buftext; end; Message1(parser_c_macro_undefined,mac.name); mac.is_used:=true; diff --git a/compiler/symsym.pas b/compiler/symsym.pas index ca9f8a8c72..e46cb8fbb1 100644 --- a/compiler/symsym.pas +++ b/compiler/symsym.pas @@ -469,9 +469,6 @@ interface maxmacrolen=16*1024; type - pmacrobuffer = ^tmacrobuffer; - tmacrobuffer = array[0..maxmacrolen-1] of char; - tmacro = class(tstoredsym) {Normally true, but false when a previously defined macro is undef-ed} defined : boolean; @@ -490,6 +487,8 @@ interface override ppuwrite_platform instead } procedure ppuwrite(ppufile:tcompilerppufile);override;final; destructor destroy;override; + function allocate_buftext(len:longint) : pchar; + procedure free_buftext; function GetCopy:tmacro; end; @@ -3114,10 +3113,7 @@ implementation is_used:=false; buflen:= ppufile.getlongint; if buflen > 0 then - begin - getmem(buftext, buflen); - ppufile.getdata(buftext^, buflen) - end + ppufile.getdata(allocate_buftext(buflen)^, buflen) else buftext:=nil; end; @@ -3141,6 +3137,27 @@ implementation end; + function tmacro.allocate_buftext(len:longint) : pchar; + begin + result:=getmem(len); + if assigned(buftext) then + freemem(buftext); + buftext:=result; + buflen:=len; + end; + + + procedure tmacro.free_buftext; + begin + if assigned(buftext) then + begin + freemem(buftext); + buftext:=nil; + buflen:=0; + end; + end; + + function tmacro.GetCopy:tmacro; var p : tmacro; @@ -3151,10 +3168,7 @@ implementation p.is_compiler_var:=is_compiler_var; p.buflen:=buflen; if assigned(buftext) then - begin - getmem(p.buftext,buflen); - move(buftext^,p.buftext^,buflen); - end; + move(buftext^,p.allocate_buftext(buflen)^,buflen); Result:=p; end; diff --git a/compiler/symtable.pas b/compiler/symtable.pas index 33213cd20c..8fb10909d1 100644 --- a/compiler/symtable.pas +++ b/compiler/symtable.pas @@ -4919,13 +4919,10 @@ implementation else begin mac.is_compiler_var:=false; - if assigned(mac.buftext) then - freemem(mac.buftext,mac.buflen); + mac.free_buftext; end; Message2(parser_c_macro_set_to,mac.name,value); - mac.buflen:=length(value); - getmem(mac.buftext,mac.buflen); - move(value[1],mac.buftext^,mac.buflen); + move(value[1],mac.allocate_buftext(length(value))^,length(value)); mac.defined:=true; end; @@ -4949,15 +4946,9 @@ implementation initialmacrosymtable.insertsym(mac); end else - begin - mac.is_compiler_var:=true; - if assigned(mac.buftext) then - freemem(mac.buftext,mac.buflen); - end; + mac.is_compiler_var:=true; Message2(parser_c_macro_set_to,mac.name,value); - mac.buflen:=length(value); - getmem(mac.buftext,mac.buflen); - move(value[1],mac.buftext^,mac.buflen); + move(value[1],mac.allocate_buftext(length(value))^,length(value)); mac.defined:=true; end; @@ -4979,11 +4970,7 @@ implementation mac.defined:=false; mac.is_compiler_var:=false; { delete old definition } - if assigned(mac.buftext) then - begin - freemem(mac.buftext,mac.buflen); - mac.buftext:=nil; - end; + mac.free_buftext; end; end;