Optimize {$DEFINE x := y} and refactor tmacro.buftext.

This commit is contained in:
Rika Ichinose 2023-03-22 13:37:47 +03:00 committed by FPK
parent 2a50d5abf8
commit 2596af84fa
3 changed files with 39 additions and 64 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;