* leb128 support for binary writers

git-svn-id: trunk@3049 -
This commit is contained in:
peter 2006-03-26 21:12:33 +00:00
parent 8a71767125
commit e7a419cbff
5 changed files with 106 additions and 46 deletions

View File

@ -109,6 +109,8 @@ interface
function LengthUleb128(a: aword) : byte; function LengthUleb128(a: aword) : byte;
function LengthSleb128(a: aint) : byte; function LengthSleb128(a: aint) : byte;
function EncodeUleb128(a: aword;out buf) : byte;
function EncodeSleb128(a: aint;out buf) : byte;
implementation implementation
@ -133,15 +135,10 @@ implementation
function LengthUleb128(a: aword) : byte; function LengthUleb128(a: aword) : byte;
var
b: byte;
begin begin
result:=0; result:=0;
repeat repeat
b := a and $7f;
a := a shr 7; a := a shr 7;
if a<>0 then
b := b or $80;
inc(result); inc(result);
if a=0 then if a=0 then
break; break;
@ -152,6 +149,7 @@ implementation
function LengthSleb128(a: aint) : byte; function LengthSleb128(a: aint) : byte;
var var
b, size: byte; b, size: byte;
asign : aint;
neg, more: boolean; neg, more: boolean;
begin begin
more := true; more := true;
@ -162,7 +160,67 @@ implementation
b := a and $7f; b := a and $7f;
a := a shr 7; a := a shr 7;
if neg then if neg then
a := a or -(1 shl (size - 7)); begin
{ Use a variable to be sure that the correct or mask is generated }
asign:=1;
asign:=asign shl (size - 7);
a := a or -asign;
end;
if (((a = 0) and
(b and $40 = 0)) or
((a = -1) and
(b and $40 <> 0))) then
more := false;
inc(result);
if not(more) then
break;
until false;
end;
function EncodeUleb128(a: aword;out buf) : byte;
var
b: byte;
pbuf : pbyte;
begin
result:=0;
pbuf:=@buf;
repeat
b := a and $7f;
a := a shr 7;
if a<>0 then
b := b or $80;
pbuf^:=b;
inc(pbuf);
inc(result);
if a=0 then
break;
until false;
end;
function EncodeSleb128(a: aint;out buf) : byte;
var
b, size: byte;
asign : aint;
neg, more: boolean;
pbuf : pbyte;
begin
more := true;
neg := a < 0;
size := sizeof(a)*8;
result:=0;
pbuf:=@buf;
repeat
b := a and $7f;
a := a shr 7;
if neg then
begin
{ Use a variable to be sure that the correct or mask is generated }
asign:=1;
asign:=asign shl (size - 7);
a := a or -asign;
end;
if (((a = 0) and if (((a = 0) and
(b and $40 = 0)) or (b and $40 = 0)) or
((a = -1) and ((a = -1) and
@ -170,6 +228,7 @@ implementation
more := false more := false
else else
b := b or $80; b := b or $80;
pbuf^:=b;
inc(result); inc(result);
if not(more) then if not(more) then
break; break;

View File

@ -1306,6 +1306,12 @@ implementation
aitconst_indirect_symbol, aitconst_indirect_symbol,
aitconst_rva_symbol : aitconst_rva_symbol :
result:=sizeof(aint); result:=sizeof(aint);
aitconst_uleb128bit :
result:=LengthUleb128(value);
aitconst_sleb128bit :
result:=LengthSleb128(value);
else
internalerror(200603253);
end; end;
end; end;

View File

@ -328,48 +328,31 @@ implementation
procedure TGNUAssembler.WriteDecodedUleb128(a: aword); procedure TGNUAssembler.WriteDecodedUleb128(a: aword);
var var
b: byte; i,len : longint;
buf : array[0..63] of byte;
begin begin
repeat len:=EncodeUleb128(a,buf);
b := a and $7f; for i:=0 to len-1 do
a := a shr 7; begin
if (a <> 0) then if (i > 0) then
b := b or $80; AsmWrite(',');
AsmWrite(tostr(b)); AsmWrite(tostr(buf[i]));
if (a <> 0) then end;
AsmWrite(',')
else
break;
until false;
end; end;
procedure TGNUAssembler.WriteDecodedSleb128(a: aint); procedure TGNUAssembler.WriteDecodedSleb128(a: aint);
var var
b, size: byte; i,len : longint;
neg, more: boolean; buf : array[0..255] of byte;
begin begin
more := true; len:=EncodeSleb128(a,buf);
neg := a < 0; for i:=0 to len-1 do
size := sizeof(a)*8; begin
repeat if (i > 0) then
b := a and $7f; AsmWrite(',');
a := a shr 7; AsmWrite(tostr(buf[i]));
if (neg) then end;
a := a or -(1 shl (size - 7));
if (((a = 0) and
(b and $40 = 0)) or
((a = -1) and
(b and $40 <> 0))) then
more := false
else
b := b or $80;
AsmWrite(tostr(b));
if (more) then
AsmWrite(',')
else
break;
until false;
end; end;

View File

@ -1053,6 +1053,8 @@ Implementation
{$endif x86} {$endif x86}
objsym, objsym,
objsymend : TObjSymbol; objsymend : TObjSymbol;
leblen : byte;
lebbuf : array[0..63] of byte;
begin begin
inlinelevel:=0; inlinelevel:=0;
{ main loop } { main loop }
@ -1123,6 +1125,18 @@ Implementation
end; end;
aitconst_rva_symbol : aitconst_rva_symbol :
ObjData.writereloc(Tai_const(hp).value,sizeof(aint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA); ObjData.writereloc(Tai_const(hp).value,sizeof(aint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
aitconst_uleb128bit :
begin
leblen:=EncodeUleb128(Tai_const(hp).value,lebbuf);
ObjData.writebytes(lebbuf,leblen);
end;
aitconst_sleb128bit :
begin
leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
ObjData.writebytes(lebbuf,leblen);
end;
else
internalerror(200603254);
end; end;
end; end;
ait_label : ait_label :
@ -1354,8 +1368,6 @@ Implementation
to_do:=[low(Tasmlisttype)..high(Tasmlisttype)]; to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
if usedeffileforexports then if usedeffileforexports then
exclude(to_do,al_exports); exclude(to_do,al_exports);
{$warning TODO internal writer support for dwarf}
exclude(to_do,al_dwarf);
if not(tf_section_threadvars in target_info.flags) then if not(tf_section_threadvars in target_info.flags) then
exclude(to_do,al_threadvars); exclude(to_do,al_threadvars);
for i:=low(TasmlistType) to high(TasmlistType) do for i:=low(TasmlistType) to high(TasmlistType) do

View File

@ -1181,7 +1181,7 @@ implementation
asmbin : ''; asmbin : '';
asmcmd : ''; asmcmd : '';
supported_target : system_any; //target_i386_linux; supported_target : system_any; //target_i386_linux;
flags : [af_outputbinary,af_smartlink_sections]; flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
labelprefix : '.L'; labelprefix : '.L';
comment : ''; comment : '';
); );
@ -1195,7 +1195,7 @@ implementation
asmbin : ''; asmbin : '';
asmcmd : ''; asmcmd : '';
supported_target : system_any; //target_i386_linux; supported_target : system_any; //target_i386_linux;
flags : [af_outputbinary,af_smartlink_sections]; flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
labelprefix : '.L'; labelprefix : '.L';
comment : ''; comment : '';
); );
@ -1210,7 +1210,7 @@ implementation
asmcmd : ''; asmcmd : '';
supported_target : system_any; //target_i386_linux; supported_target : system_any; //target_i386_linux;
// flags : [af_outputbinary,af_smartlink_sections]; // flags : [af_outputbinary,af_smartlink_sections];
flags : [af_outputbinary]; flags : [af_outputbinary,af_supports_dwarf];
labelprefix : '.L'; labelprefix : '.L';
comment : ''; comment : '';
); );