123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- {
- $Id$
- Copyright (c) 1998-2000 by Peter Vreman
- This unit handles the assemblerfile write and assembler calls of FPC
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit assemble;
- {$i defines.inc}
- interface
- uses
- {$ifdef Delphi}
- sysutils,
- dmisc,
- {$else Delphi}
- strings,
- dos,
- {$endif Delphi}
- cobjects,globtype,globals,aasm;
- const
- AsmOutSize=32768;
- type
- PAsmList=^TAsmList;
- TAsmList=object
- private
- procedure CreateSmartLinkPath(const s:string);
- public
- {filenames}
- path : pathstr;
- name : namestr;
- asmfile, { current .s and .o file }
- objfile,
- as_bin : string;
- SmartAsm : boolean;
- SmartFilesCount,
- SmartHeaderCount : longint;
- place : TCutPlace; { special 'end' file for import dir ? }
- {outfile}
- AsmSize,
- AsmStartSize,
- outcnt : longint;
- outbuf : array[0..AsmOutSize-1] of char;
- outfile : file;
- Constructor Init(smart:boolean);
- Destructor Done;
- Function FindAssembler:string;
- Function CallAssembler(const command,para:string):Boolean;
- Function DoAssemble:boolean;
- Procedure RemoveAsm;
- procedure NextSmartName;
- Procedure AsmFlush;
- Procedure AsmClear;
- Procedure AsmWrite(const s:string);
- Procedure AsmWritePChar(p:pchar);
- Procedure AsmWriteLn(const s:string);
- Procedure AsmLn;
- procedure AsmCreate(Aplace:tcutplace);
- procedure AsmClose;
- procedure Synchronize;
- procedure WriteTree(p:TAAsmoutput);virtual;
- procedure WriteAsmList;virtual;
- end;
- Procedure GenerateAsm(smart:boolean);
- Procedure OnlyAsm;
- Implementation
- uses
- cutils,script,fmodule,systems,verbose
- {$ifdef unix}
- ,unix
- {$endif}
- {$ifdef i386}
- {$ifndef NoAg386Bin}
- ,ag386bin
- {$endif}
- {$ifndef NoAg386Att}
- ,ag386att
- {$endif NoAg386Att}
- {$ifndef NoAg386Nsm}
- ,ag386nsm
- {$endif NoAg386Nsm}
- {$ifndef NoAg386Int}
- ,ag386int
- {$endif NoAg386Int}
- {$ifdef Ag386Cof}
- ,ag386cof
- {$endif Ag386Cof}
- {$endif}
- {$ifdef m68k}
- {$ifndef NoAg68kGas}
- ,ag68kgas
- {$endif NoAg68kGas}
- {$ifndef NoAg68kMot}
- ,ag68kmot
- {$endif NoAg68kMot}
- {$ifndef NoAg68kMit}
- ,ag68kmit
- {$endif NoAg68kMit}
- {$ifndef NoAg68kMpw}
- ,ag68kmpw
- {$endif NoAg68kMpw}
- {$endif}
- ;
- {*****************************************************************************
- TAsmList
- *****************************************************************************}
- Function DoPipe:boolean;
- begin
- DoPipe:=(cs_asm_pipe in aktglobalswitches) and
- not(cs_asm_leave in aktglobalswitches)
- {$ifdef i386}
- and (aktoutputformat=as_i386_as)
- {$endif i386}
- {$ifdef m68k}
- and (aktoutputformat=as_m68k_as);
- {$endif m68k}
- end;
- const
- lastas : byte=255;
- var
- LastASBin : pathstr;
- Function TAsmList.FindAssembler:string;
- var
- asfound : boolean;
- UtilExe : string;
- begin
- UtilExe:=AddExtension(target_asm.asmbin,source_os.exeext);
- if lastas<>ord(target_asm.id) then
- begin
- lastas:=ord(target_asm.id);
- { is an assembler passed ? }
- if utilsdirectory<>'' then
- LastASBin:=FindFile(UtilExe,utilsdirectory,asfound)+UtilExe;
- if not AsFound then
- LastASBin:=FindExe(UtilExe,asfound);
- if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
- begin
- Message1(exec_w_assembler_not_found,LastASBin);
- aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
- end;
- if asfound then
- Message1(exec_t_using_assembler,LastASBin);
- end;
- FindAssembler:=LastASBin;
- end;
- Function TAsmList.CallAssembler(const command,para:string):Boolean;
- begin
- callassembler:=true;
- if not(cs_asm_extern in aktglobalswitches) then
- begin
- swapvectors;
- exec(command,para);
- swapvectors;
- if (doserror<>0) then
- begin
- Message1(exec_w_cant_call_assembler,tostr(doserror));
- aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
- callassembler:=false;
- end
- else
- if (dosexitcode<>0) then
- begin
- Message1(exec_w_error_while_assembling,tostr(dosexitcode));
- callassembler:=false;
- end;
- end
- else
- AsmRes.AddAsmCommand(command,para,name);
- end;
- procedure TAsmList.RemoveAsm;
- var
- g : file;
- begin
- if cs_asm_leave in aktglobalswitches then
- exit;
- if cs_asm_extern in aktglobalswitches then
- AsmRes.AddDeleteCommand(AsmFile)
- else
- begin
- assign(g,AsmFile);
- {$I-}
- erase(g);
- {$I+}
- if ioresult<>0 then;
- end;
- end;
- Function TAsmList.DoAssemble:boolean;
- var
- s : string;
- begin
- DoAssemble:=true;
- if DoPipe then
- exit;
- if not(cs_asm_extern in aktglobalswitches) then
- begin
- if SmartAsm then
- begin
- if (SmartFilesCount<=1) then
- Message1(exec_i_assembling_smart,name);
- end
- else
- Message1(exec_i_assembling,name);
- end;
- s:=target_asm.asmcmd;
- Replace(s,'$ASM',AsmFile);
- Replace(s,'$OBJ',ObjFile);
- if CallAssembler(FindAssembler,s) then
- RemoveAsm
- else
- begin
- DoAssemble:=false;
- GenerateError;
- end;
- end;
- procedure TAsmList.NextSmartName;
- var
- s : string;
- begin
- inc(SmartFilesCount);
- if SmartFilesCount>999999 then
- Message(asmw_f_too_many_asm_files);
- case place of
- cut_begin :
- begin
- inc(SmartHeaderCount);
- s:=current_module.asmprefix^+tostr(SmartHeaderCount)+'h';
- end;
- cut_normal :
- s:=current_module.asmprefix^+tostr(SmartHeaderCount)+'s';
- cut_end :
- s:=current_module.asmprefix^+tostr(SmartHeaderCount)+'t';
- end;
- AsmFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
- ObjFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
- { insert in container so it can be cleared after the linking }
- SmartLinkOFiles.Insert(Objfile);
- end;
- {*****************************************************************************
- TAsmList AsmFile Writing
- *****************************************************************************}
- Procedure TAsmList.AsmFlush;
- begin
- if outcnt>0 then
- begin
- BlockWrite(outfile,outbuf,outcnt);
- outcnt:=0;
- end;
- end;
- Procedure TAsmList.AsmClear;
- begin
- outcnt:=0;
- end;
- Procedure TAsmList.AsmWrite(const s:string);
- begin
- if OutCnt+length(s)>=AsmOutSize then
- AsmFlush;
- Move(s[1],OutBuf[OutCnt],length(s));
- inc(OutCnt,length(s));
- inc(AsmSize,length(s));
- end;
- Procedure TAsmList.AsmWriteLn(const s:string);
- begin
- AsmWrite(s);
- AsmLn;
- end;
- Procedure TAsmList.AsmWritePChar(p:pchar);
- var
- i,j : longint;
- begin
- i:=StrLen(p);
- j:=i;
- while j>0 do
- begin
- i:=min(j,AsmOutSize);
- if OutCnt+i>=AsmOutSize then
- AsmFlush;
- Move(p[0],OutBuf[OutCnt],i);
- inc(OutCnt,i);
- inc(AsmSize,i);
- dec(j,i);
- p:=pchar(@p[i]);
- end;
- end;
- Procedure TAsmList.AsmLn;
- begin
- if OutCnt>=AsmOutSize-2 then
- AsmFlush;
- OutBuf[OutCnt]:=target_os.newline[1];
- inc(OutCnt);
- inc(AsmSize);
- if length(target_os.newline)>1 then
- begin
- OutBuf[OutCnt]:=target_os.newline[2];
- inc(OutCnt);
- inc(AsmSize);
- end;
- end;
- procedure TAsmList.AsmCreate(Aplace:tcutplace);
- begin
- place:=Aplace;
- if SmartAsm then
- NextSmartName;
- {$ifdef unix}
- if DoPipe then
- begin
- Message1(exec_i_assembling_pipe,asmfile);
- POpen(outfile,'as -o '+objfile,'W');
- end
- else
- {$endif}
- begin
- Assign(outfile,asmfile);
- {$I-}
- Rewrite(outfile,1);
- {$I+}
- if ioresult<>0 then
- Message1(exec_d_cant_create_asmfile,asmfile);
- end;
- outcnt:=0;
- AsmSize:=0;
- AsmStartSize:=0;
- end;
- procedure TAsmList.AsmClose;
- var
- f : file;
- l : longint;
- begin
- AsmFlush;
- {$ifdef unix}
- if DoPipe then
- Close(outfile)
- else
- {$endif}
- begin
- {Touch Assembler time to ppu time is there is a ppufilename}
- if Assigned(current_module.ppufilename) then
- begin
- Assign(f,current_module.ppufilename^);
- {$I-}
- reset(f,1);
- {$I+}
- if ioresult=0 then
- begin
- getftime(f,l);
- close(f);
- reset(outfile,1);
- setftime(outfile,l);
- end;
- end;
- close(outfile);
- end;
- end;
- {Touch Assembler and object time to ppu time is there is a ppufilename}
- procedure TAsmList.Synchronize;
- begin
- {Touch Assembler time to ppu time is there is a ppufilename}
- if Assigned(current_module.ppufilename) then
- begin
- SynchronizeFileTime(current_module.ppufilename^,asmfile);
- if not(cs_asm_extern in aktglobalswitches) then
- SynchronizeFileTime(current_module.ppufilename^,objfile);
- end;
- end;
- procedure TAsmList.WriteTree(p:TAAsmoutput);
- begin
- end;
- procedure TAsmList.WriteAsmList;
- begin
- end;
- procedure TAsmList.CreateSmartLinkPath(const s:string);
- var
- dir : searchrec;
- begin
- if PathExists(s) then
- begin
- { the path exists, now we clean only all the .o and .s files }
- { .o files }
- findfirst(s+dirsep+'*'+target_info.objext,anyfile,dir);
- while (doserror=0) do
- begin
- RemoveFile(s+dirsep+dir.name);
- findnext(dir);
- end;
- findclose(dir);
- { .s files }
- findfirst(s+dirsep+'*'+target_info.asmext,anyfile,dir);
- while (doserror=0) do
- begin
- RemoveFile(s+dirsep+dir.name);
- findnext(dir);
- end;
- findclose(dir);
- end
- else
- begin
- {$I-}
- mkdir(s);
- {$I+}
- if ioresult<>0 then;
- end;
- end;
- Constructor TAsmList.Init(smart:boolean);
- begin
- { load start values }
- asmfile:=current_module.asmfilename^;
- objfile:=current_module.objfilename^;
- name:=FixFileName(current_module.modulename^);
- OutCnt:=0;
- SmartFilesCount:=0;
- SmartLinkOFiles.Clear;
- place:=cut_normal;
- SmartAsm:=smart;
- SmartHeaderCount:=0;
- { Which path will be used ? }
- if SmartAsm then
- begin
- path:=current_module.outputpath^+FixFileName(current_module.modulename^)+target_info.smartext;
- CreateSmartLinkPath(path);
- path:=FixPath(path,false);
- end
- else
- path:=current_module.outputpath^;
- end;
- Destructor TAsmList.Done;
- begin
- end;
- {*****************************************************************************
- Generate Assembler Files Main Procedure
- *****************************************************************************}
- Procedure GenerateAsm(smart:boolean);
- var
- a : PAsmList;
- {$ifdef i386}
- {$ifndef NoAg386Bin}
- b : Pi386binasmlist;
- {$endif}
- {$endif}
- begin
- case aktoutputformat of
- as_none : ;
- {$ifdef i386}
- {$ifndef NoAg386Bin}
- as_i386_dbg,
- as_i386_coff,
- as_i386_pecoff,
- as_i386_elf :
- begin
- case aktoutputformat of
- as_i386_dbg :
- b:=new(pi386binasmlist,Init(og_dbg,smart));
- as_i386_coff :
- b:=new(pi386binasmlist,Init(og_coff,smart));
- as_i386_pecoff :
- b:=new(pi386binasmlist,Init(og_pecoff,smart));
- as_i386_elf :
- b:=new(pi386binasmlist,Init(og_elf,smart));
- end;
- b^.WriteBin;
- dispose(b,done);
- if assigned(current_module.ppufilename) then
- begin
- if smart then
- SynchronizeFileTime(current_module.ppufilename^,current_module.staticlibfilename^)
- else
- SynchronizeFileTime(current_module.ppufilename^,current_module.objfilename^);
- end;
- exit;
- end;
- {$endif NoAg386Bin}
- {$ifndef NoAg386Att}
- as_i386_as,
- as_i386_as_aout,
- as_i386_asw :
- a:=new(pi386attasmlist,Init(smart));
- {$endif NoAg386Att}
- {$ifndef NoAg386Nsm}
- as_i386_nasmcoff,
- as_i386_nasmwin32,
- as_i386_nasmelf,
- as_i386_nasmobj :
- a:=new(pi386nasmasmlist,Init(smart));
- {$endif NoAg386Nsm}
- {$ifndef NoAg386Int}
- as_i386_tasm :
- a:=new(pi386intasmlist,Init(smart));
- {$endif NoAg386Int}
- {$endif}
- {$ifdef m68k}
- {$ifndef NoAg68kGas}
- as_m68k_as,
- as_m68k_gas :
- a:=new(pm68kgasasmlist,Init(smart));
- {$endif NoAg86KGas}
- {$ifndef NoAg68kMot}
- as_m68k_mot :
- a:=new(pm68kmotasmlist,Init(smart));
- {$endif NoAg86kMot}
- {$ifndef NoAg68kMit}
- as_m68k_mit :
- a:=new(pm68kmitasmlist,Init(smart));
- {$endif NoAg86KMot}
- {$ifndef NoAg68kMpw}
- as_m68k_mpw :
- a:=new(pm68kmpwasmlist,Init(smart));
- {$endif NoAg68kMpw}
- {$endif}
- else
- Message(asmw_f_assembler_output_not_supported);
- end;
- a^.AsmCreate(cut_normal);
- a^.WriteAsmList;
- a^.AsmClose;
- a^.DoAssemble;
- a^.synchronize;
- dispose(a,Done);
- end;
- Procedure OnlyAsm;
- var
- a : PAsmList;
- begin
- a:=new(pasmlist,Init(false));
- a^.DoAssemble;
- dispose(a,Done);
- end;
- end.
- {
- $Log$
- Revision 1.10 2001-01-21 20:32:45 marco
- * Renamefest. Compiler part. Not that hard.
- Revision 1.9 2001/01/12 19:19:44 peter
- * fixed searching for utils
- Revision 1.8 2000/12/25 00:07:25 peter
- + new tlinkedlist class (merge of old tstringqueue,tcontainer and
- tlinkedlist objects)
- Revision 1.7 2000/11/13 15:26:12 marco
- * Renamefest
- Revision 1.6 2000/10/01 19:48:23 peter
- * lot of compile updates for cg11
- Revision 1.5 2000/09/24 15:06:11 peter
- * use defines.inc
- Revision 1.4 2000/08/27 16:11:49 peter
- * moved some util functions from globals,cobjects to cutils
- * splitted files into finput,fmodule
- Revision 1.3 2000/07/13 12:08:24 michael
- + patched to 1.1.0 with former 1.09patch from peter
- Revision 1.2 2000/07/13 11:32:32 michael
- + removed logs
- }
|