操作系统学习初步-gcc输出汇编标号处理程序
当用Ada语言或C语言写的代码输出汇编文件后,多个文件中可能存在着重复的标号。这将导致用as汇编器同时编译多个文件时出错,如下:
LFE10:
LFB10:
L10:
下列Ada程序将前2种标号去除,第3种标号加 _文件名 前缀,这样不同的文件名前缀将可以避免冲突。
with ada.Command_Line; use ada.Command_Line;
with ada.Text_IO;
with ada.Streams.Stream_IO; use ada.Streams.Stream_IO;
with ada.Unchecked_Deallocation;
procedure RemoveSign is
type ASTR is access all string;
procedure Free is new ada.Unchecked_Deallocation(string,ASTR);
function load(file:in string) return ASTR is
use ada.Streams.Stream_IO;
pFile:File_Type;
fsize:count;
St :Stream_Access;
src :ASTR:=Null;
begin
open(pFile,in_file,file);
fsize:=size(pFile);
St:=stream(pFile);
src:=new string(1..Integer(fsize));
string'read(St,Src.all);
close(pFile);
return src;
exception when others=>
return Null;
end load;
function GetName(S:string) return string is
last:Integer;
first:Integer:=-1;
begin
for i in reverse S'range loop
if S(i)='.' then
last:=i-1;
elsif S(i)='/' or S(i)='\' then
first:=i+1;
exit;
end if;
end loop;
if first=-1 then
first:=S'first;
end if;
declare
s0:string(1..last-first+4):="__"&s(first..last)&"_";
begin
return S0;
end;
end GetName;
begin
if Argument_Count<=1 then
ada.Text_IO.Put_Line("Remove unused sign in asm source file");
ada.Text_IO.put_line("Useage:RemoveSign File1 File2...");
else
for i in 1..Argument_Count loop
declare
File1:ada.Streams.Stream_IO.File_Type;
S1:Stream_Access;
Src :ASTR:=load(Argument(i));
FN:string:=GetName(Argument(i));
begin
open(File1,out_File,Argument(i));
S1:=Stream(File1);
for j in src'range loop
if j+2<=src'last and then (src(j..j+2)="LFE" or src(j..j+2)="LFB") and then (src(j-1)=ASCII.CR or src(j-1)=ASCII.LF) then
for k in j..src'last loop
if src(k)=':' then
src(k):=' ';
exit;
else
src(k):=' ';
end if;
end loop;
end if;
end loop;
for j in src'range loop
if src(j)/='L' then
character'write(s1,src(j));
else
if (j-1>=src'first and then (src(j-1)=' ' or src(j-1)=character'val(9) or src(j-1)=ASCII.CR or src(j-1)=ASCII.LF)) and (j+1<=src'last and then src(j+1) in '0'..'9') then
string'write(S1,FN&src(j));
else
character'write(s1,src(j));
end if;
end if;
end loop;
close(File1);
end;
end loop;
end if;
end RemoveSign;