123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464 |
- {
- Copyright (c) 1998-2004 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.
- ****************************************************************************
- }
- {# @abstract(This unit handles the assembler file write and assembler calls of FPC)
- Handles the calls to the actual external assemblers, as well as the generation
- of object files for smart linking. Also contains the base class for writing
- the assembler statements to file.
- }
- unit assemble;
- {$i fpcdefs.inc}
- interface
- uses
- {$IFDEF USE_SYSUTILS}
- sysutils,
- {$ELSE USE_SYSUTILS}
- strings,
- dos,
- {$ENDIF USE_SYSUTILS}
- systems,globtype,globals,aasmbase,aasmtai,aasmdata,ogbase;
- const
- { maximum of aasmoutput lists there will be }
- maxoutputlists = 20;
- { buffer size for writing the .s file }
- AsmOutSize=32768;
- type
- TAssembler=class(TAbstractAssembler)
- public
- {filenames}
- path : pathstr;
- name : namestr;
- AsmFileName, { current .s and .o file }
- ObjFileName,
- ppufilename : string;
- asmprefix : string;
- SmartAsm : boolean;
- SmartFilesCount,
- SmartHeaderCount : longint;
- Constructor Create(smart:boolean);virtual;
- Destructor Destroy;override;
- procedure NextSmartName(place:tcutplace);
- procedure MakeObject;virtual;abstract;
- end;
- {# This is the base class which should be overriden for each each
- assembler writer. It is used to actually assembler a file,
- and write the output to the assembler file.
- }
- TExternalAssembler=class(TAssembler)
- private
- procedure CreateSmartLinkPath(const s:string);
- protected
- {outfile}
- AsmSize,
- AsmStartSize,
- outcnt : longint;
- outbuf : array[0..AsmOutSize-1] of char;
- outfile : file;
- ioerror : boolean;
- public
- {# Returns the complete path and executable name of the assembler
- program.
- It first tries looking in the UTIL directory if specified,
- otherwise it searches in the free pascal binary directory, in
- the current working directory and then in the directories
- in the $PATH environment.}
- Function FindAssembler:string;
- {# Actually does the call to the assembler file. Returns false
- if the assembling of the file failed.}
- Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
- Function DoAssemble:boolean;virtual;
- Procedure RemoveAsm;
- Procedure AsmFlush;
- Procedure AsmClear;
- {# Write a string to the assembler file }
- Procedure AsmWrite(const s:string);
- {# Write a string to the assembler file }
- Procedure AsmWritePChar(p:pchar);
- {# Write a string to the assembler file followed by a new line }
- Procedure AsmWriteLn(const s:string);
- {# Write a new line to the assembler file }
- Procedure AsmLn;
- procedure AsmCreate(Aplace:tcutplace);
- procedure AsmClose;
- {# This routine should be overriden for each assembler, it is used
- to actually write the abstract assembler stream to file.}
- procedure WriteTree(p:TAsmList);virtual;
- {# This routine should be overriden for each assembler, it is used
- to actually write all the different abstract assembler streams
- by calling for each stream type, the @var(WriteTree) method.}
- procedure WriteAsmList;virtual;
- {# Constructs the command line for calling the assembler }
- function MakeCmdLine: TCmdStr;
- public
- Constructor Create(smart:boolean);override;
- procedure MakeObject;override;
- end;
- TInternalAssembler=class(TAssembler)
- private
- FCObjOutput : TObjOutputclass;
- { the aasmoutput lists that need to be processed }
- lists : byte;
- list : array[1..maxoutputlists] of TAsmList;
- { current processing }
- currlistidx : byte;
- currlist : TAsmList;
- procedure convertstab(p:pchar);
- function MaybeNextList(var hp:Tai):boolean;
- function TreePass0(hp:Tai):Tai;
- function TreePass1(hp:Tai):Tai;
- function TreePass2(hp:Tai):Tai;
- procedure writetree;
- procedure writetreesmart;
- protected
- ObjData : TObjData;
- ObjOutput : tObjOutput;
- property CObjOutput:TObjOutputclass read FCObjOutput write FCObjOutput;
- public
- constructor create(smart:boolean);override;
- destructor destroy;override;
- procedure MakeObject;override;
- end;
- TAssemblerClass = class of TAssembler;
- Procedure GenerateAsm(smart:boolean);
- Procedure OnlyAsm;
- procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
- procedure InitAssembler;
- procedure DoneAssembler;
- Implementation
- uses
- {$ifdef hasunix}
- {$ifdef havelinuxrtl10}
- linux,
- {$else}
- unix,
- {$endif}
- {$endif}
- cutils,script,fmodule,verbose,
- {$ifdef memdebug}
- cclasses,
- {$endif memdebug}
- {$ifdef m68k}
- cpuinfo,
- {$endif m68k}
- aasmcpu,
- owbase,owar
- ;
- var
- CAssembler : array[tasm] of TAssemblerClass;
- {*****************************************************************************
- TAssembler
- *****************************************************************************}
- Constructor TAssembler.Create(smart:boolean);
- begin
- { load start values }
- AsmFileName:=current_module.get_AsmFilename;
- ObjFileName:=current_module.ObjFileName^;
- name:=Lower(current_module.modulename^);
- path:=current_module.outputpath^;
- asmprefix := current_module.asmprefix^;
- if not assigned(current_module.outputpath) then
- ppufilename := ''
- else
- ppufilename := current_module.ppufilename^;
- SmartAsm:=smart;
- SmartFilesCount:=0;
- SmartHeaderCount:=0;
- SmartLinkOFiles.Clear;
- end;
- Destructor TAssembler.Destroy;
- begin
- end;
- procedure TAssembler.NextSmartName(place:tcutplace);
- 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:=asmprefix+tostr(SmartHeaderCount)+'h';
- end;
- cut_normal :
- s:=asmprefix+tostr(SmartHeaderCount)+'s';
- cut_end :
- s:=asmprefix+tostr(SmartHeaderCount)+'t';
- end;
- AsmFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
- ObjFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
- { insert in container so it can be cleared after the linking }
- SmartLinkOFiles.Insert(ObjFileName);
- end;
- {*****************************************************************************
- TExternalAssembler
- *****************************************************************************}
- Function DoPipe:boolean;
- begin
- DoPipe:=(cs_asm_pipe in aktglobalswitches) and
- (([cs_asm_leave,cs_link_on_target] * aktglobalswitches) = []) and
- ((target_asm.id in [as_gas,as_darwin]));
- end;
- Constructor TExternalAssembler.Create(smart:boolean);
- begin
- inherited Create(smart);
- if SmartAsm then
- begin
- path:=FixPath(path+FixFileName(name)+target_info.smartext,false);
- CreateSmartLinkPath(path);
- end;
- Outcnt:=0;
- end;
- procedure TExternalAssembler.CreateSmartLinkPath(const s:string);
- var
- {$IFDEF USE_SYSUTILS}
- dir : TSearchRec;
- {$ELSE USE_SYSUTILS}
- dir : searchrec;
- {$ENDIF USE_SYSUTILS}
- hs : string;
- begin
- if PathExists(s) then
- begin
- { the path exists, now we clean only all the .o and .s files }
- { .o files }
- {$IFDEF USE_SYSUTILS}
- if findfirst(s+source_info.dirsep+'*'+target_info.objext,faAnyFile,dir) = 0
- then repeat
- RemoveFile(s+source_info.dirsep+dir.name);
- until findnext(dir) <> 0;
- {$ELSE USE_SYSUTILS}
- findfirst(s+source_info.dirsep+'*'+target_info.objext,anyfile,dir);
- while (doserror=0) do
- begin
- RemoveFile(s+source_info.dirsep+dir.name);
- findnext(dir);
- end;
- {$ENDIF USE_SYSUTILS}
- findclose(dir);
- { .s files }
- {$IFDEF USE_SYSUTILS}
- if findfirst(s+source_info.dirsep+'*'+target_info.asmext,faAnyFile,dir) = 0
- then repeat
- RemoveFile(s+source_info.dirsep+dir.name);
- until findnext(dir) <> 0;
- {$ELSE USE_SYSUTILS}
- findfirst(s+source_info.dirsep+'*'+target_info.asmext,anyfile,dir);
- while (doserror=0) do
- begin
- RemoveFile(s+source_info.dirsep+dir.name);
- findnext(dir);
- end;
- {$ENDIF USE_SYSUTILS}
- findclose(dir);
- end
- else
- begin
- hs:=s;
- if hs[length(hs)] in ['/','\'] then
- delete(hs,length(hs),1);
- {$I-}
- mkdir(hs);
- {$I+}
- if ioresult<>0 then;
- end;
- end;
- const
- lastas : byte=255;
- var
- LastASBin : pathstr;
- Function TExternalAssembler.FindAssembler:string;
- var
- asfound : boolean;
- UtilExe : string;
- begin
- asfound:=false;
- if cs_link_on_target in aktglobalswitches then
- begin
- { If linking on target, don't add any path PM }
- FindAssembler:=utilsprefix+AddExtension(target_asm.asmbin,target_info.exeext);
- exit;
- end
- else
- UtilExe:=utilsprefix+AddExtension(target_asm.asmbin,source_info.exeext);
- if lastas<>ord(target_asm.id) then
- begin
- lastas:=ord(target_asm.id);
- { is an assembler passed ? }
- if utilsdirectory<>'' then
- asfound:=FindFile(UtilExe,utilsdirectory,LastASBin);
- if not AsFound then
- asfound:=FindExe(UtilExe,LastASBin);
- if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
- begin
- Message1(exec_e_assembler_not_found,LastASBin);
- aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
- end;
- if asfound then
- Message1(exec_t_using_assembler,LastASBin);
- end;
- FindAssembler:=LastASBin;
- end;
- Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
- {$IFDEF USE_SYSUTILS}
- var
- DosExitCode:Integer;
- {$ENDIF USE_SYSUTILS}
- begin
- callassembler:=true;
- if not(cs_asm_extern in aktglobalswitches) then
- {$IFDEF USE_SYSUTILS}
- try
- DosExitCode := ExecuteProcess(command,para);
- if DosExitCode <>0
- then begin
- Message1(exec_e_error_while_assembling,tostr(dosexitcode));
- callassembler:=false;
- end;
- except on E:EOSError do
- begin
- Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
- aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
- callassembler:=false;
- end
- end
- {$ELSE USE_SYSUTILS}
- begin
- swapvectors;
- exec(maybequoted(command),para);
- swapvectors;
- if (doserror<>0) then
- begin
- Message1(exec_e_cant_call_assembler,tostr(doserror));
- aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
- callassembler:=false;
- end
- else
- if (dosexitcode<>0) then
- begin
- Message1(exec_e_error_while_assembling,tostr(dosexitcode));
- callassembler:=false;
- end;
- end
- {$ENDIF USE_SYSUTILS}
- else
- AsmRes.AddAsmCommand(command,para,name);
- end;
- procedure TExternalAssembler.RemoveAsm;
- var
- g : file;
- begin
- if cs_asm_leave in aktglobalswitches then
- exit;
- if cs_asm_extern in aktglobalswitches then
- AsmRes.AddDeleteCommand(AsmFileName)
- else
- begin
- assign(g,AsmFileName);
- {$I-}
- erase(g);
- {$I+}
- if ioresult<>0 then;
- end;
- end;
- Function TExternalAssembler.DoAssemble:boolean;
- 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;
- if CallAssembler(FindAssembler,MakeCmdLine) then
- RemoveAsm
- else
- begin
- DoAssemble:=false;
- GenerateError;
- end;
- end;
- Procedure TExternalAssembler.AsmFlush;
- begin
- if outcnt>0 then
- begin
- { suppress i/o error }
- {$i-}
- BlockWrite(outfile,outbuf,outcnt);
- {$i+}
- ioerror:=ioerror or (ioresult<>0);
- outcnt:=0;
- end;
- end;
- Procedure TExternalAssembler.AsmClear;
- begin
- outcnt:=0;
- end;
- Procedure TExternalAssembler.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 TExternalAssembler.AsmWriteLn(const s:string);
- begin
- AsmWrite(s);
- AsmLn;
- end;
- Procedure TExternalAssembler.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 TExternalAssembler.AsmLn;
- begin
- if OutCnt>=AsmOutSize-2 then
- AsmFlush;
- if (cs_link_on_target in aktglobalswitches) then
- begin
- OutBuf[OutCnt]:=target_info.newline[1];
- inc(OutCnt);
- inc(AsmSize);
- if length(target_info.newline)>1 then
- begin
- OutBuf[OutCnt]:=target_info.newline[2];
- inc(OutCnt);
- inc(AsmSize);
- end;
- end
- else
- begin
- OutBuf[OutCnt]:=source_info.newline[1];
- inc(OutCnt);
- inc(AsmSize);
- if length(source_info.newline)>1 then
- begin
- OutBuf[OutCnt]:=source_info.newline[2];
- inc(OutCnt);
- inc(AsmSize);
- end;
- end;
- end;
- function TExternalAssembler.MakeCmdLine: TCmdStr;
- begin
- result:=target_asm.asmcmd;
- {$ifdef m68k}
- if aktcputype = cpu_MC68020 then
- result:='-m68020 '+result
- else
- result:='-m68000 '+result;
- {$endif}
- if (cs_link_on_target in aktglobalswitches) then
- begin
- Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
- Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
- end
- else
- begin
- {$ifdef hasunix}
- if DoPipe then
- Replace(result,'$ASM','')
- else
- {$endif}
- Replace(result,'$ASM',maybequoted(AsmFileName));
- Replace(result,'$OBJ',maybequoted(ObjFileName));
- end;
- end;
- procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
- begin
- if SmartAsm then
- NextSmartName(Aplace);
- {$ifdef hasunix}
- if DoPipe then
- begin
- if SmartAsm then
- begin
- if (SmartFilesCount<=1) then
- Message1(exec_i_assembling_smart,name);
- end
- else
- Message1(exec_i_assembling_pipe,AsmFileName);
- POpen(outfile,FindAssembler+' '+MakeCmdLine,'W');
- end
- else
- {$endif}
- begin
- Assign(outfile,AsmFileName);
- {$I-}
- Rewrite(outfile,1);
- {$I+}
- if ioresult<>0 then
- begin
- ioerror:=true;
- Message1(exec_d_cant_create_asmfile,AsmFileName);
- end;
- end;
- outcnt:=0;
- AsmSize:=0;
- AsmStartSize:=0;
- end;
- procedure TExternalAssembler.AsmClose;
- var
- f : file;
- FileAge : longint;
- begin
- AsmFlush;
- {$ifdef hasunix}
- if DoPipe then
- begin
- if PClose(outfile) <> 0 then
- GenerateError;
- end
- else
- {$endif}
- begin
- {Touch Assembler time to ppu time is there is a ppufilename}
- if ppufilename<>'' then
- begin
- Assign(f,ppufilename);
- {$I-}
- reset(f,1);
- {$I+}
- if ioresult=0 then
- begin
- {$IFDEF USE_SYSUTILS}
- FileAge := FileGetDate(GetFileHandle(f));
- {$ELSE USE_SYSUTILS}
- GetFTime(f, FileAge);
- {$ENDIF USE_SYSUTILS}
- close(f);
- reset(outfile,1);
- {$IFDEF USE_SYSUTILS}
- FileSetDate(GetFileHandle(outFile),FileAge);
- {$ELSE USE_SYSUTILS}
- SetFTime(f, FileAge);
- {$ENDIF USE_SYSUTILS}
- end;
- end;
- close(outfile);
- end;
- end;
- procedure TExternalAssembler.WriteTree(p:TAsmList);
- begin
- end;
- procedure TExternalAssembler.WriteAsmList;
- begin
- end;
- procedure TExternalAssembler.MakeObject;
- begin
- AsmCreate(cut_normal);
- WriteAsmList;
- AsmClose;
- if not(ioerror) then
- DoAssemble;
- end;
- {*****************************************************************************
- TInternalAssembler
- *****************************************************************************}
- constructor TInternalAssembler.create(smart:boolean);
- begin
- inherited create(smart);
- ObjOutput:=nil;
- ObjData:=nil;
- SmartAsm:=smart;
- end;
- destructor TInternalAssembler.destroy;
- {$ifdef MEMDEBUG}
- var
- d : tmemdebug;
- {$endif}
- begin
- {$ifdef MEMDEBUG}
- d := tmemdebug.create(name+' - agbin');
- {$endif}
- if assigned(ObjData) then
- ObjData.free;
- if assigned(ObjOutput) then
- ObjOutput.free;
- {$ifdef MEMDEBUG}
- d.free;
- {$endif}
- end;
- procedure TInternalAssembler.convertstab(p:pchar);
- function consumecomma(var p:pchar):boolean;
- begin
- while (p^=' ') do
- inc(p);
- result:=(p^=',');
- inc(p);
- end;
- function consumenumber(var p:pchar;out value:longint):boolean;
- var
- hs : string;
- len,
- code : integer;
- begin
- value:=0;
- while (p^=' ') do
- inc(p);
- len:=0;
- while (p^ in ['0'..'9']) do
- begin
- inc(len);
- hs[len]:=p^;
- inc(p);
- end;
- if len>0 then
- begin
- hs[0]:=chr(len);
- val(hs,value,code);
- end
- else
- code:=-1;
- result:=(code=0);
- end;
- function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
- var
- hs : string;
- len,
- code : integer;
- pstart : pchar;
- sym : tobjsymbol;
- exprvalue : longint;
- gotmin,
- dosub : boolean;
- begin
- result:=false;
- value:=0;
- relocsym:=nil;
- gotmin:=false;
- repeat
- dosub:=false;
- exprvalue:=0;
- if gotmin then
- begin
- dosub:=true;
- gotmin:=false;
- end;
- while (p^=' ') do
- inc(p);
- case p^ of
- #0 :
- break;
- ' ' :
- inc(p);
- '0'..'9' :
- begin
- len:=0;
- while (p^ in ['0'..'9']) do
- begin
- inc(len);
- hs[len]:=p^;
- inc(p);
- end;
- hs[0]:=chr(len);
- val(hs,exprvalue,code);
- end;
- '.','_',
- 'A'..'Z',
- 'a'..'z' :
- begin
- pstart:=p;
- while not(p^ in [#0,' ','-','+']) do
- inc(p);
- len:=p-pstart;
- if len>255 then
- internalerror(200509187);
- move(pstart^,hs[1],len);
- hs[0]:=chr(len);
- sym:=objdata.symbolref(hs);
- { Second symbol? }
- if assigned(relocsym) then
- begin
- if (relocsym.objsection<>sym.objsection) then
- internalerror(2005091810);
- relocsym:=nil;
- end
- else
- relocsym:=sym;
- exprvalue:=sym.address;
- end;
- '+' :
- begin
- { nothing, by default addition is done }
- inc(p);
- end;
- '-' :
- begin
- gotmin:=true;
- inc(p);
- end;
- else
- internalerror(200509189);
- end;
- if dosub then
- dec(value,exprvalue)
- else
- inc(value,exprvalue);
- until false;
- result:=true;
- end;
- const
- N_Function = $24; { function or const }
- var
- ofs,
- nline,
- nidx,
- nother,
- i : longint;
- relocsym : TObjSymbol;
- pstr,
- pcurr,
- pendquote : pchar;
- begin
- pcurr:=nil;
- pstr:=nil;
- pendquote:=nil;
- { Parse string part }
- if p[0]='"' then
- begin
- pstr:=@p[1];
- { Ignore \" inside the string }
- i:=1;
- while not((p[i]='"') and (p[i-1]<>'\')) and
- (p[i]<>#0) do
- inc(i);
- pendquote:=@p[i];
- pendquote^:=#0;
- pcurr:=@p[i+1];
- if not consumecomma(pcurr) then
- internalerror(200509181);
- end
- else
- pcurr:=p;
- { When in pass 1 then only alloc and leave }
- if ObjData.currpass=1 then
- ObjData.allocstab(pstr)
- else
- begin
- { Stabs format: nidx,nother,nline[,offset] }
- if not consumenumber(pcurr,nidx) then
- internalerror(200509182);
- if not consumecomma(pcurr) then
- internalerror(200509183);
- if not consumenumber(pcurr,nother) then
- internalerror(200509184);
- if not consumecomma(pcurr) then
- internalerror(200509185);
- if not consumenumber(pcurr,nline) then
- internalerror(200509186);
- if consumecomma(pcurr) then
- consumeoffset(pcurr,relocsym,ofs)
- else
- begin
- ofs:=0;
- relocsym:=nil;
- end;
- if assigned(relocsym) and
- (relocsym.bind=AB_GLOBAL) and
- (nidx=N_Function) and
- (tf_use_function_relative_addresses in target_info.flags) then
- ofs:=0;
- ObjData.writestab(ofs,relocsym,byte(nidx),byte(nother),word(nline),pstr);
- end;
- if assigned(pendquote) then
- pendquote^:='"';
- end;
- function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
- begin
- { maybe end of list }
- while not assigned(hp) do
- begin
- if currlistidx<lists then
- begin
- inc(currlistidx);
- currlist:=list[currlistidx];
- hp:=Tai(currList.first);
- end
- else
- begin
- MaybeNextList:=false;
- exit;
- end;
- end;
- MaybeNextList:=true;
- end;
- function TInternalAssembler.TreePass0(hp:Tai):Tai;
- begin
- while assigned(hp) do
- begin
- case hp.typ of
- ait_align :
- begin
- { always use the maximum fillsize in this pass to avoid possible
- short jumps to become out of range }
- Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
- ObjData.alloc(Tai_align_abstract(hp).fillsize);
- end;
- ait_datablock :
- begin
- ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
- ObjData.SymbolDefine(Tai_datablock(hp).sym);
- ObjData.alloc(Tai_datablock(hp).size);
- end;
- ait_real_80bit :
- ObjData.alloc(10);
- ait_real_64bit :
- ObjData.alloc(8);
- ait_real_32bit :
- ObjData.alloc(4);
- ait_comp_64bit :
- ObjData.alloc(8);
- ait_const:
- ObjData.alloc(tai_const(hp).size);
- ait_section:
- begin
- ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^);
- Tai_section(hp).sec:=ObjData.CurrObjSec;
- end;
- ait_symbol :
- ObjData.SymbolDefine(Tai_symbol(hp).sym);
- ait_label :
- ObjData.SymbolDefine(Tai_label(hp).labsym);
- ait_string :
- ObjData.alloc(Tai_string(hp).len);
- ait_instruction :
- begin
- { reset instructions which could change in pass 2 }
- Taicpu(hp).resetpass2;
- ObjData.alloc(Taicpu(hp).Pass1(ObjData));
- end;
- ait_cutobject :
- if SmartAsm then
- break;
- end;
- hp:=Tai(hp.next);
- end;
- TreePass0:=hp;
- end;
- function TInternalAssembler.TreePass1(hp:Tai):Tai;
- var
- InlineLevel : longint;
- objsym : TObjSymbol;
- begin
- inlinelevel:=0;
- while assigned(hp) do
- begin
- case hp.typ of
- ait_align :
- begin
- { here we must determine the fillsize which is used in pass2 }
- Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
- ObjData.CurrObjSec.Size;
- ObjData.alloc(Tai_align_abstract(hp).fillsize);
- end;
- ait_datablock :
- begin
- if (oso_data in ObjData.CurrObjSec.secoptions) then
- Message(asmw_e_alloc_data_only_in_bss);
- ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
- objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
- objsym.size:=Tai_datablock(hp).size;
- ObjData.alloc(Tai_datablock(hp).size);
- end;
- ait_real_80bit :
- ObjData.alloc(10);
- ait_real_64bit :
- ObjData.alloc(8);
- ait_real_32bit :
- ObjData.alloc(4);
- ait_comp_64bit :
- ObjData.alloc(8);
- ait_const:
- begin
- ObjData.alloc(tai_const(hp).size);
- if assigned(Tai_const(hp).sym) then
- ObjData.SymbolRef(Tai_const(hp).sym);
- if assigned(Tai_const(hp).endsym) then
- ObjData.SymbolRef(Tai_const(hp).endsym);
- end;
- ait_section:
- begin
- { use cached value }
- ObjData.setsection(Tai_section(hp).sec);
- end;
- ait_stab :
- begin
- if assigned(Tai_stab(hp).str) then
- convertstab(Tai_stab(hp).str);
- end;
- ait_symbol :
- ObjData.SymbolDefine(Tai_symbol(hp).sym);
- ait_symbol_end :
- begin
- objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
- objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
- end;
- ait_label :
- ObjData.SymbolDefine(Tai_label(hp).labsym);
- ait_string :
- ObjData.alloc(Tai_string(hp).len);
- ait_instruction :
- ObjData.alloc(Taicpu(hp).Pass1(ObjData));
- ait_cutobject :
- if SmartAsm then
- break;
- ait_marker :
- if tai_marker(hp).kind=mark_InlineStart then
- inc(InlineLevel)
- else if tai_marker(hp).kind=mark_InlineEnd then
- dec(InlineLevel);
- end;
- hp:=Tai(hp.next);
- end;
- TreePass1:=hp;
- end;
- function TInternalAssembler.TreePass2(hp:Tai):Tai;
- var
- fillbuffer : tfillbuffer;
- InlineLevel,
- v : int64;
- {$ifdef x86}
- co : comp;
- {$endif x86}
- hs : string;
- objsym,
- objsymparent,
- objsymend : TObjSymbol;
- leblen : byte;
- lebbuf : array[0..63] of byte;
- begin
- inlinelevel:=0;
- { main loop }
- while assigned(hp) do
- begin
- case hp.typ of
- ait_align :
- begin
- if (oso_data in ObjData.CurrObjSec.secoptions) then
- ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer)^,Tai_align_abstract(hp).fillsize)
- else
- ObjData.alloc(Tai_align_abstract(hp).fillsize);
- end;
- ait_section :
- begin
- { use cached value }
- ObjData.setsection(Tai_section(hp).sec);
- end;
- ait_symbol :
- begin
- ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
- end;
- ait_datablock :
- begin
- ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
- ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
- ObjData.alloc(Tai_datablock(hp).size);
- end;
- ait_real_80bit :
- ObjData.writebytes(Tai_real_80bit(hp).value,10);
- ait_real_64bit :
- ObjData.writebytes(Tai_real_64bit(hp).value,8);
- ait_real_32bit :
- ObjData.writebytes(Tai_real_32bit(hp).value,4);
- ait_comp_64bit :
- begin
- {$ifdef x86}
- co:=comp(Tai_comp_64bit(hp).value);
- ObjData.writebytes(co,8);
- {$endif x86}
- end;
- ait_string :
- ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
- ait_const :
- begin
- case tai_const(hp).consttype of
- aitconst_64bit,
- aitconst_32bit,
- aitconst_16bit,
- aitconst_8bit :
- begin
- if assigned(tai_const(hp).sym) then
- begin
- objsym:=Objdata.SymbolRef(tai_const(hp).sym);
- if assigned(tai_const(hp).endsym) then
- begin
- objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
- if objsymend.objsection<>objsym.objsection then
- internalerror(200404124);
- v:=objsymend.address-objsym.address+Tai_const(hp).value;
- ObjData.writebytes(v,tai_const(hp).size);
- end
- else
- ObjData.writereloc(Tai_const(hp).value,Tai_const(hp).size,objsym,RELOC_ABSOLUTE);
- end
- else
- ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
- end;
- aitconst_rva_symbol :
- { PE32+? }
- if target_info.system=system_x86_64_win64 then
- ObjData.writereloc(Tai_const(hp).value,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
- else
- 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;
- ait_label :
- begin
- { exporting shouldn't be necessary as labels are local,
- but it's better to be on the safe side (PFV) }
- ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
- end;
- ait_instruction :
- Taicpu(hp).Pass2(ObjData);
- ait_stab :
- convertstab(Tai_stab(hp).str);
- ait_function_name,
- ait_force_line : ;
- ait_cutobject :
- if SmartAsm then
- break;
- ait_marker :
- if tai_marker(hp).kind=mark_InlineStart then
- inc(InlineLevel)
- else if tai_marker(hp).kind=mark_InlineEnd then
- dec(InlineLevel);
- end;
- hp:=Tai(hp.next);
- end;
- TreePass2:=hp;
- end;
- procedure TInternalAssembler.writetree;
- label
- doexit;
- var
- hp : Tai;
- ObjWriter : TObjectWriter;
- begin
- ObjWriter:=TObjectwriter.create;
- ObjOutput:=CObjOutput.Create(ObjWriter);
- ObjData:=ObjOutput.newObjData(ObjFileName);
- { Pass 0 }
- ObjData.currpass:=0;
- ObjData.createsection(sec_code,'');
- ObjData.beforealloc;
- { start with list 1 }
- currlistidx:=1;
- currlist:=list[currlistidx];
- hp:=Tai(currList.first);
- while assigned(hp) do
- begin
- hp:=TreePass0(hp);
- MaybeNextList(hp);
- end;
- ObjData.afteralloc;
- { leave if errors have occured }
- if errorcount>0 then
- goto doexit;
- { Pass 1 }
- ObjData.currpass:=1;
- ObjData.resetsections;
- ObjData.beforealloc;
- ObjData.createsection(sec_code,'');
- { start with list 1 }
- currlistidx:=1;
- currlist:=list[currlistidx];
- hp:=Tai(currList.first);
- while assigned(hp) do
- begin
- hp:=TreePass1(hp);
- MaybeNextList(hp);
- end;
- ObjData.createsection(sec_code,'');
- ObjData.afteralloc;
- { leave if errors have occured }
- if errorcount>0 then
- goto doexit;
- { Pass 2 }
- ObjData.currpass:=2;
- ObjData.resetsections;
- ObjData.beforewrite;
- ObjData.createsection(sec_code,'');
- { start with list 1 }
- currlistidx:=1;
- currlist:=list[currlistidx];
- hp:=Tai(currList.first);
- while assigned(hp) do
- begin
- hp:=TreePass2(hp);
- MaybeNextList(hp);
- end;
- ObjData.createsection(sec_code,'');
- ObjData.afterwrite;
- { don't write the .o file if errors have occured }
- if errorcount=0 then
- begin
- { write objectfile }
- ObjOutput.startobjectfile(ObjFileName);
- ObjOutput.writeobjectfile(ObjData);
- end;
- doexit:
- { Cleanup }
- ObjData.free;
- ObjData:=nil;
- ObjWriter.free;
- end;
- procedure TInternalAssembler.writetreesmart;
- var
- hp : Tai;
- startsectype : TAsmSectiontype;
- place: tcutplace;
- ObjWriter : TObjectWriter;
- begin
- if not(cs_asm_leave in aktglobalswitches) then
- ObjWriter:=TARObjectWriter.create(current_module.staticlibfilename^)
- else
- ObjWriter:=TObjectwriter.create;
- NextSmartName(cut_normal);
- ObjOutput:=CObjOutput.Create(ObjWriter);
- startsectype:=sec_code;
- { start with list 1 }
- currlistidx:=1;
- currlist:=list[currlistidx];
- hp:=Tai(currList.first);
- while assigned(hp) do
- begin
- ObjData:=ObjOutput.newObjData(ObjFileName);
- { Pass 0 }
- ObjData.currpass:=0;
- ObjData.resetsections;
- ObjData.beforealloc;
- ObjData.createsection(startsectype,'');
- TreePass0(hp);
- ObjData.afteralloc;
- { leave if errors have occured }
- if errorcount>0 then
- break;
- { Pass 1 }
- ObjData.currpass:=1;
- ObjData.resetsections;
- ObjData.beforealloc;
- ObjData.createsection(startsectype,'');
- TreePass1(hp);
- ObjData.afteralloc;
- { leave if errors have occured }
- if errorcount>0 then
- break;
- { Pass 2 }
- ObjData.currpass:=2;
- ObjOutput.startobjectfile(ObjFileName);
- ObjData.resetsections;
- ObjData.beforewrite;
- ObjData.createsection(startsectype,'');
- hp:=TreePass2(hp);
- ObjData.afterwrite;
- { leave if errors have occured }
- if errorcount>0 then
- break;
- { write the current objectfile }
- ObjOutput.writeobjectfile(ObjData);
- ObjData.free;
- ObjData:=nil;
- { end of lists? }
- if not MaybeNextList(hp) then
- break;
- { we will start a new objectfile so reset everything }
- { The place can still change in the next while loop, so don't init }
- { the writer yet (JM) }
- if (hp.typ=ait_cutobject) then
- place := Tai_cutobject(hp).place
- else
- place := cut_normal;
- { avoid empty files }
- startsectype:=sec_code;
- while assigned(hp) and
- (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
- begin
- if Tai(hp).typ=ait_section then
- startsectype:=Tai_section(hp).sectype;
- if (Tai(hp).typ=ait_cutobject) then
- place:=Tai_cutobject(hp).place;
- hp:=Tai(hp.next);
- end;
- if not MaybeNextList(hp) then
- break;
- { start next objectfile }
- NextSmartName(place);
- end;
- ObjData.free;
- ObjData:=nil;
- ObjWriter.free;
- end;
- procedure TInternalAssembler.MakeObject;
- var to_do:set of TasmlistType;
- i:TasmlistType;
- {$ifdef MEMDEBUG}
- d : tmemdebug;
- {$endif}
- procedure addlist(p:TAsmList);
- begin
- inc(lists);
- list[lists]:=p;
- end;
- begin
- to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
- if usedeffileforexports then
- exclude(to_do,al_exports);
- if not(tf_section_threadvars in target_info.flags) then
- exclude(to_do,al_threadvars);
- for i:=low(TasmlistType) to high(TasmlistType) do
- if (i in to_do) and (current_asmdata.asmlists[i]<>nil) then
- addlist(current_asmdata.asmlists[i]);
- if SmartAsm then
- writetreesmart
- else
- writetree;
- (*
- if assigned(objectlibrary) then
- begin
- if objectlibrary<>current_module.librarydata then
- internalerror(200603013);
- {$ifdef MEMDEBUG}
- d:=tmemdebug.create(modulename^+' - librarydata');
- {$endif}
- current_asmdata.free;
- objectlibrary:=nil;
- current_module.librarydata:=nil;
- {$ifdef MEMDEBUG}
- d.free;
- {$endif}
- end;
- *)
- end;
- {*****************************************************************************
- Generate Assembler Files Main Procedure
- *****************************************************************************}
- Procedure GenerateAsm(smart:boolean);
- var
- a : TAssembler;
- begin
- if not assigned(CAssembler[target_asm.id]) then
- Message(asmw_f_assembler_output_not_supported);
- a:=CAssembler[target_asm.id].Create(smart);
- a.MakeObject;
- a.Free;
- end;
- Procedure OnlyAsm;
- var
- a : TExternalAssembler;
- begin
- a:=TExternalAssembler.Create(false);
- a.DoAssemble;
- a.Free;
- end;
- {*****************************************************************************
- Init/Done
- *****************************************************************************}
- procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
- var
- t : tasm;
- begin
- t:=r.id;
- if assigned(asminfos[t]) then
- writeln('Warning: Assembler is already registered!')
- else
- Getmem(asminfos[t],sizeof(tasminfo));
- asminfos[t]^:=r;
- CAssembler[t]:=c;
- end;
- procedure InitAssembler;
- begin
- end;
- procedure DoneAssembler;
- begin
- end;
- end.
|