assemble.pas 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  1. {
  2. Copyright (c) 1998-2004 by Peter Vreman
  3. This unit handles the assemblerfile write and assembler calls of FPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# @abstract(This unit handles the assembler file write and assembler calls of FPC)
  18. Handles the calls to the actual external assemblers, as well as the generation
  19. of object files for smart linking. Also contains the base class for writing
  20. the assembler statements to file.
  21. }
  22. unit assemble;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. SysUtils,
  27. systems,globtype,globals,aasmbase,aasmtai,aasmdata,ogbase,owbase,finput;
  28. const
  29. { maximum of aasmoutput lists there will be }
  30. maxoutputlists = ord(high(tasmlisttype))+1;
  31. { buffer size for writing the .s file }
  32. AsmOutSize=32768*4;
  33. type
  34. TAssembler=class(TObject)
  35. public
  36. {assembler info}
  37. asminfo : pasminfo;
  38. {filenames}
  39. path : TPathStr;
  40. name : string;
  41. AsmFileName, { current .s and .o file }
  42. ObjFileName,
  43. ppufilename : TPathStr;
  44. asmprefix : string;
  45. SmartAsm : boolean;
  46. SmartFilesCount,
  47. SmartHeaderCount : longint;
  48. Constructor Create(info: pasminfo; smart:boolean);virtual;
  49. Destructor Destroy;override;
  50. procedure NextSmartName(place:tcutplace);
  51. procedure MakeObject;virtual;abstract;
  52. end;
  53. TExternalAssembler = class;
  54. IExternalAssemblerOutputFileDecorator=interface
  55. function LinePrefix: AnsiString;
  56. function LinePostfix: AnsiString;
  57. function LineFilter(const s: AnsiString): AnsiString;
  58. function LineEnding(const deflineending: ShortString): ShortString;
  59. end;
  60. TExternalAssemblerOutputFile=class
  61. private
  62. fdecorator: IExternalAssemblerOutputFileDecorator;
  63. protected
  64. owner: TExternalAssembler;
  65. {outfile}
  66. AsmSize,
  67. AsmStartSize,
  68. outcnt : longint;
  69. outbuf : array[0..AsmOutSize-1] of char;
  70. outfile : file;
  71. fioerror : boolean;
  72. linestart: boolean;
  73. Procedure AsmClear;
  74. Procedure MaybeAddLinePrefix;
  75. Procedure MaybeAddLinePostfix;
  76. Procedure AsmWriteAnsiStringUnfiltered(const s: ansistring);
  77. public
  78. Constructor Create(_owner: TExternalAssembler);
  79. Procedure RemoveAsm;virtual;
  80. Procedure AsmFlush;
  81. { mark the current output as the "empty" state (i.e., it only contains
  82. headers/directives etc }
  83. Procedure MarkEmpty;
  84. { clears the assembler output if nothing was added since it was marked
  85. as empty, and returns whether it was empty }
  86. function ClearIfEmpty: boolean;
  87. { these routines will write the filtered version of their argument
  88. according to the current decorator }
  89. procedure AsmWriteFiltered(const c:char);
  90. procedure AsmWriteFiltered(const s:string);
  91. procedure AsmWriteFiltered(const s:ansistring);
  92. procedure AsmWriteFiltered(p:pchar; len: longint);
  93. {# Write a string to the assembler file }
  94. Procedure AsmWrite(const c:char);
  95. Procedure AsmWrite(const s:string);
  96. Procedure AsmWrite(const s:ansistring);
  97. {# Write a string to the assembler file }
  98. Procedure AsmWritePChar(p:pchar);
  99. {# Write a string to the assembler file followed by a new line }
  100. Procedure AsmWriteLn(const c:char);
  101. Procedure AsmWriteLn(const s:string);
  102. Procedure AsmWriteLn(const s:ansistring);
  103. {# Write a new line to the assembler file }
  104. Procedure AsmLn; virtual;
  105. procedure AsmCreate(Aplace:tcutplace);
  106. procedure AsmClose;
  107. property ioerror: boolean read fioerror;
  108. property decorator: IExternalAssemblerOutputFileDecorator read fdecorator write fdecorator;
  109. end;
  110. {# This is the base class which should be overridden for each each
  111. assembler writer. It is used to actually assembler a file,
  112. and write the output to the assembler file.
  113. }
  114. TExternalAssembler=class(TAssembler)
  115. private
  116. { output writer }
  117. fwriter: TExternalAssemblerOutputFile;
  118. ffreewriter: boolean;
  119. procedure CreateSmartLinkPath(const s:TPathStr);
  120. protected
  121. {input source info}
  122. lastfileinfo : tfileposinfo;
  123. infile,
  124. lastinfile : tinputfile;
  125. {last section type written}
  126. lastsectype : TAsmSectionType;
  127. procedure WriteSourceLine(hp: tailineinfo);
  128. procedure WriteTempalloc(hp: tai_tempalloc);
  129. procedure WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  130. function single2str(d : single) : string; virtual;
  131. function double2str(d : double) : string; virtual;
  132. function extended2str(e : extended) : string; virtual;
  133. Function DoPipe:boolean;
  134. function CreateNewAsmWriter: TExternalAssemblerOutputFile; virtual;
  135. public
  136. {# Returns the complete path and executable name of the assembler
  137. program.
  138. It first tries looking in the UTIL directory if specified,
  139. otherwise it searches in the free pascal binary directory, in
  140. the current working directory and then in the directories
  141. in the $PATH environment.}
  142. Function FindAssembler:string;
  143. {# Actually does the call to the assembler file. Returns false
  144. if the assembling of the file failed.}
  145. Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
  146. Function DoAssemble:boolean;virtual;
  147. {# This routine should be overridden for each assembler, it is used
  148. to actually write the abstract assembler stream to file.}
  149. procedure WriteTree(p:TAsmList);virtual;
  150. {# This routine should be overridden for each assembler, it is used
  151. to actually write all the different abstract assembler streams
  152. by calling for each stream type, the @var(WriteTree) method.}
  153. procedure WriteAsmList;virtual;
  154. {# Constructs the command line for calling the assembler }
  155. function MakeCmdLine: TCmdStr; virtual;
  156. public
  157. Constructor Create(info: pasminfo; smart: boolean); override; final;
  158. Constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); virtual;
  159. procedure MakeObject;override;
  160. destructor Destroy; override;
  161. property writer: TExternalAssemblerOutputFile read fwriter;
  162. end;
  163. TExternalAssemblerClass = class of TExternalAssembler;
  164. { TInternalAssembler }
  165. TInternalAssembler=class(TAssembler)
  166. private
  167. FCObjOutput : TObjOutputclass;
  168. FCInternalAr : TObjectWriterClass;
  169. { the aasmoutput lists that need to be processed }
  170. lists : byte;
  171. list : array[1..maxoutputlists] of TAsmList;
  172. { current processing }
  173. currlistidx : byte;
  174. currlist : TAsmList;
  175. procedure WriteStab(p:pchar);
  176. function MaybeNextList(var hp:Tai):boolean;
  177. function SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  178. function TreePass0(hp:Tai):Tai;
  179. function TreePass1(hp:Tai):Tai;
  180. function TreePass2(hp:Tai):Tai;
  181. procedure writetree;
  182. procedure writetreesmart;
  183. protected
  184. ObjData : TObjData;
  185. ObjOutput : tObjOutput;
  186. property CObjOutput:TObjOutputclass read FCObjOutput write FCObjOutput;
  187. property CInternalAr : TObjectWriterClass read FCInternalAr write FCInternalAr;
  188. public
  189. constructor Create(info: pasminfo; smart: boolean);override;
  190. destructor destroy;override;
  191. procedure MakeObject;override;
  192. end;
  193. TAssemblerClass = class of TAssembler;
  194. Procedure GenerateAsm(smart:boolean);
  195. { get an instance of an external GNU-style assembler that is compatible
  196. with the current target, reusing an existing writer. Used by the LLVM
  197. target to write inline assembler }
  198. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  199. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  200. Implementation
  201. uses
  202. {$ifdef hasunix}
  203. unix,
  204. {$endif}
  205. cutils,cfileutl,
  206. {$ifdef memdebug}
  207. cclasses,
  208. {$endif memdebug}
  209. {$ifdef OMFOBJSUPPORT}
  210. omfbase,
  211. ogomf,
  212. {$endif OMFOBJSUPPORT}
  213. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  214. {$else}
  215. {$ifdef FPC_SOFT_FPUX80}
  216. sfpux80,
  217. {$endif FPC_SOFT_FPUX80}
  218. {$endif}
  219. cscript,fmodule,verbose,
  220. cpuinfo,
  221. aasmcpu;
  222. var
  223. CAssembler : array[tasm] of TAssemblerClass;
  224. function fixline(s:string):string;
  225. {
  226. return s with all leading and ending spaces and tabs removed
  227. }
  228. var
  229. i,j,k : integer;
  230. begin
  231. i:=length(s);
  232. while (i>0) and (s[i] in [#9,' ']) do
  233. dec(i);
  234. j:=1;
  235. while (j<i) and (s[j] in [#9,' ']) do
  236. inc(j);
  237. for k:=j to i do
  238. if s[k] in [#0..#31,#127..#255] then
  239. s[k]:='.';
  240. fixline:=Copy(s,j,i-j+1);
  241. end;
  242. {*****************************************************************************
  243. TAssembler
  244. *****************************************************************************}
  245. Constructor TAssembler.Create(info: pasminfo; smart: boolean);
  246. begin
  247. asminfo:=info;
  248. { load start values }
  249. AsmFileName:=current_module.AsmFilename;
  250. ObjFileName:=current_module.ObjFileName;
  251. name:=Lower(current_module.modulename^);
  252. path:=current_module.outputpath;
  253. asmprefix := current_module.asmprefix^;
  254. if current_module.outputpath = '' then
  255. ppufilename := ''
  256. else
  257. ppufilename := current_module.ppufilename;
  258. SmartAsm:=smart;
  259. SmartFilesCount:=0;
  260. SmartHeaderCount:=0;
  261. SmartLinkOFiles.Clear;
  262. end;
  263. Destructor TAssembler.Destroy;
  264. begin
  265. end;
  266. procedure TAssembler.NextSmartName(place:tcutplace);
  267. var
  268. s : string;
  269. begin
  270. inc(SmartFilesCount);
  271. if SmartFilesCount>999999 then
  272. Message(asmw_f_too_many_asm_files);
  273. case place of
  274. cut_begin :
  275. begin
  276. inc(SmartHeaderCount);
  277. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  278. end;
  279. cut_normal :
  280. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  281. cut_end :
  282. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  283. end;
  284. AsmFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  285. ObjFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  286. { insert in container so it can be cleared after the linking }
  287. SmartLinkOFiles.Insert(ObjFileName);
  288. end;
  289. {*****************************************************************************
  290. TAssemblerOutputFile
  291. *****************************************************************************}
  292. procedure TExternalAssemblerOutputFile.RemoveAsm;
  293. var
  294. g : file;
  295. begin
  296. if cs_asm_leave in current_settings.globalswitches then
  297. exit;
  298. if cs_asm_extern in current_settings.globalswitches then
  299. AsmRes.AddDeleteCommand(owner.AsmFileName)
  300. else
  301. begin
  302. assign(g,owner.AsmFileName);
  303. {$push} {$I-}
  304. erase(g);
  305. {$pop}
  306. if ioresult<>0 then;
  307. end;
  308. end;
  309. Procedure TExternalAssemblerOutputFile.AsmFlush;
  310. begin
  311. if outcnt>0 then
  312. begin
  313. { suppress i/o error }
  314. {$push} {$I-}
  315. BlockWrite(outfile,outbuf,outcnt);
  316. {$pop}
  317. fioerror:=fioerror or (ioresult<>0);
  318. outcnt:=0;
  319. end;
  320. end;
  321. procedure TExternalAssemblerOutputFile.MarkEmpty;
  322. begin
  323. AsmStartSize:=AsmSize
  324. end;
  325. function TExternalAssemblerOutputFile.ClearIfEmpty: boolean;
  326. begin
  327. result:=AsmSize=AsmStartSize;
  328. if result then
  329. AsmClear;
  330. end;
  331. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const c: char);
  332. begin
  333. MaybeAddLinePrefix;
  334. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(c));
  335. end;
  336. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const s: string);
  337. begin
  338. MaybeAddLinePrefix;
  339. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  340. end;
  341. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const s: ansistring);
  342. begin
  343. MaybeAddLinePrefix;
  344. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  345. end;
  346. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(p: pchar; len: longint);
  347. var
  348. s: ansistring;
  349. begin
  350. MaybeAddLinePrefix;
  351. s:='';
  352. setlength(s,len);
  353. move(p^,s[1],len);
  354. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  355. end;
  356. Procedure TExternalAssemblerOutputFile.AsmClear;
  357. begin
  358. outcnt:=0;
  359. end;
  360. procedure TExternalAssemblerOutputFile.MaybeAddLinePrefix;
  361. begin
  362. if assigned(decorator) and
  363. linestart then
  364. begin
  365. AsmWriteAnsiStringUnfiltered(decorator.LinePrefix);
  366. linestart:=false;
  367. end;
  368. end;
  369. procedure TExternalAssemblerOutputFile.MaybeAddLinePostfix;
  370. begin
  371. if assigned(decorator) and
  372. not linestart then
  373. begin
  374. AsmWriteAnsiStringUnfiltered(decorator.LinePostfix);
  375. linestart:=true;
  376. end;
  377. end;
  378. procedure TExternalAssemblerOutputFile.AsmWriteAnsiStringUnfiltered(const s: ansistring);
  379. var
  380. StartIndex, ToWrite: longint;
  381. begin
  382. if s='' then
  383. exit;
  384. if OutCnt+length(s)>=AsmOutSize then
  385. AsmFlush;
  386. StartIndex:=1;
  387. ToWrite:=length(s);
  388. while ToWrite>AsmOutSize do
  389. begin
  390. Move(s[StartIndex],OutBuf[OutCnt],AsmOutSize);
  391. inc(OutCnt,AsmOutSize);
  392. inc(AsmSize,AsmOutSize);
  393. AsmFlush;
  394. inc(StartIndex,AsmOutSize);
  395. dec(ToWrite,AsmOutSize);
  396. end;
  397. Move(s[StartIndex],OutBuf[OutCnt],ToWrite);
  398. inc(OutCnt,ToWrite);
  399. inc(AsmSize,ToWrite);
  400. end;
  401. constructor TExternalAssemblerOutputFile.Create(_owner: TExternalAssembler);
  402. begin
  403. owner:=_owner;
  404. linestart:=true;
  405. end;
  406. Procedure TExternalAssemblerOutputFile.AsmWrite(const c: char);
  407. begin
  408. if assigned(decorator) then
  409. AsmWriteFiltered(c)
  410. else
  411. begin
  412. if OutCnt+1>=AsmOutSize then
  413. AsmFlush;
  414. OutBuf[OutCnt]:=c;
  415. inc(OutCnt);
  416. inc(AsmSize);
  417. end;
  418. end;
  419. Procedure TExternalAssemblerOutputFile.AsmWrite(const s:string);
  420. begin
  421. if s='' then
  422. exit;
  423. if assigned(decorator) then
  424. AsmWriteFiltered(s)
  425. else
  426. begin
  427. if OutCnt+length(s)>=AsmOutSize then
  428. AsmFlush;
  429. Move(s[1],OutBuf[OutCnt],length(s));
  430. inc(OutCnt,length(s));
  431. inc(AsmSize,length(s));
  432. end;
  433. end;
  434. Procedure TExternalAssemblerOutputFile.AsmWrite(const s:ansistring);
  435. begin
  436. if s='' then
  437. exit;
  438. if assigned(decorator) then
  439. AsmWriteFiltered(s)
  440. else
  441. AsmWriteAnsiStringUnfiltered(s);
  442. end;
  443. procedure TExternalAssemblerOutputFile.AsmWriteLn(const c: char);
  444. begin
  445. AsmWrite(c);
  446. AsmLn;
  447. end;
  448. Procedure TExternalAssemblerOutputFile.AsmWriteLn(const s:string);
  449. begin
  450. AsmWrite(s);
  451. AsmLn;
  452. end;
  453. Procedure TExternalAssemblerOutputFile.AsmWriteLn(const s: ansistring);
  454. begin
  455. AsmWrite(s);
  456. AsmLn;
  457. end;
  458. Procedure TExternalAssemblerOutputFile.AsmWritePChar(p:pchar);
  459. var
  460. i,j : longint;
  461. begin
  462. i:=StrLen(p);
  463. if i=0 then
  464. exit;
  465. if assigned(decorator) then
  466. AsmWriteFiltered(p,i)
  467. else
  468. begin
  469. j:=i;
  470. while j>0 do
  471. begin
  472. i:=min(j,AsmOutSize);
  473. if OutCnt+i>=AsmOutSize then
  474. AsmFlush;
  475. Move(p[0],OutBuf[OutCnt],i);
  476. inc(OutCnt,i);
  477. inc(AsmSize,i);
  478. dec(j,i);
  479. p:=pchar(@p[i]);
  480. end;
  481. end;
  482. end;
  483. Procedure TExternalAssemblerOutputFile.AsmLn;
  484. var
  485. newline: pshortstring;
  486. newlineres: shortstring;
  487. index: longint;
  488. begin
  489. MaybeAddLinePostfix;
  490. if (cs_link_on_target in current_settings.globalswitches) then
  491. newline:=@target_info.newline
  492. else
  493. newline:=@source_info.newline;
  494. if assigned(decorator) then
  495. begin
  496. newlineres:=decorator.LineEnding(newline^);
  497. newline:=@newlineres;
  498. end;
  499. if OutCnt>=AsmOutSize-length(newline^) then
  500. AsmFlush;
  501. index:=1;
  502. repeat
  503. OutBuf[OutCnt]:=newline^[index];
  504. inc(OutCnt);
  505. inc(AsmSize);
  506. inc(index);
  507. until index>length(newline^);
  508. end;
  509. procedure TExternalAssemblerOutputFile.AsmCreate(Aplace:tcutplace);
  510. {$ifdef hasamiga}
  511. var
  512. tempFileName: TPathStr;
  513. {$endif}
  514. begin
  515. if owner.SmartAsm then
  516. owner.NextSmartName(Aplace);
  517. {$ifdef hasamiga}
  518. { on Amiga/MorphOS try to redirect .s files to the T: assign, which is
  519. for temp files, and usually (default setting) located in the RAM: drive.
  520. This highly improves assembling speed for complex projects like the
  521. compiler itself, especially on hardware with slow disk I/O.
  522. Consider this as a poor man's pipe on Amiga, because real pipe handling
  523. would be much more complex and error prone to implement. (KB) }
  524. if (([cs_asm_extern,cs_asm_leave,cs_link_on_target] * current_settings.globalswitches) = []) then
  525. begin
  526. { try to have an unique name for the .s file }
  527. tempFileName:=HexStr(GetProcessID shr 4,7)+ExtractFileName(owner.AsmFileName);
  528. {$ifndef morphos}
  529. { old Amiga RAM: handler only allows filenames up to 30 char }
  530. if Length(tempFileName) < 30 then
  531. {$endif}
  532. owner.AsmFileName:='T:'+tempFileName;
  533. end;
  534. {$endif}
  535. {$ifdef hasunix}
  536. if owner.DoPipe then
  537. begin
  538. if owner.SmartAsm then
  539. begin
  540. if (owner.SmartFilesCount<=1) then
  541. Message1(exec_i_assembling_smart,owner.name);
  542. end
  543. else
  544. Message1(exec_i_assembling_pipe,owner.AsmFileName);
  545. if checkverbosity(V_Executable) then
  546. comment(V_Executable,'Executing "'+maybequoted(owner.FindAssembler)+'" with command line "'+
  547. owner.MakeCmdLine+'"');
  548. POpen(outfile,maybequoted(owner.FindAssembler)+' '+owner.MakeCmdLine,'W');
  549. end
  550. else
  551. {$endif}
  552. begin
  553. Assign(outfile,owner.AsmFileName);
  554. {$push} {$I-}
  555. Rewrite(outfile,1);
  556. {$pop}
  557. if ioresult<>0 then
  558. begin
  559. fioerror:=true;
  560. Message1(exec_d_cant_create_asmfile,owner.AsmFileName);
  561. end;
  562. end;
  563. outcnt:=0;
  564. AsmSize:=0;
  565. AsmStartSize:=0;
  566. end;
  567. procedure TExternalAssemblerOutputFile.AsmClose;
  568. var
  569. f : file;
  570. FileAge : longint;
  571. begin
  572. AsmFlush;
  573. {$ifdef hasunix}
  574. if owner.DoPipe then
  575. begin
  576. if PClose(outfile) <> 0 then
  577. GenerateError;
  578. end
  579. else
  580. {$endif}
  581. begin
  582. {Touch Assembler time to ppu time is there is a ppufilename}
  583. if owner.ppufilename<>'' then
  584. begin
  585. Assign(f,owner.ppufilename);
  586. {$push} {$I-}
  587. reset(f,1);
  588. {$pop}
  589. if ioresult=0 then
  590. begin
  591. FileAge := FileGetDate(GetFileHandle(f));
  592. close(f);
  593. reset(outfile,1);
  594. FileSetDate(GetFileHandle(outFile),FileAge);
  595. end;
  596. end;
  597. close(outfile);
  598. end;
  599. end;
  600. {*****************************************************************************
  601. TExternalAssembler
  602. *****************************************************************************}
  603. function TExternalAssembler.single2str(d : single) : string;
  604. var
  605. hs : string;
  606. begin
  607. str(d,hs);
  608. { replace space with + }
  609. if hs[1]=' ' then
  610. hs[1]:='+';
  611. single2str:='0d'+hs
  612. end;
  613. function TExternalAssembler.double2str(d : double) : string;
  614. var
  615. hs : string;
  616. begin
  617. str(d,hs);
  618. { replace space with + }
  619. if hs[1]=' ' then
  620. hs[1]:='+';
  621. double2str:='0d'+hs
  622. end;
  623. function TExternalAssembler.extended2str(e : extended) : string;
  624. var
  625. hs : string;
  626. begin
  627. str(e,hs);
  628. { replace space with + }
  629. if hs[1]=' ' then
  630. hs[1]:='+';
  631. extended2str:='0d'+hs
  632. end;
  633. Function TExternalAssembler.DoPipe:boolean;
  634. begin
  635. DoPipe:=(cs_asm_pipe in current_settings.globalswitches) and
  636. (([cs_asm_extern,cs_asm_leave,cs_link_on_target] * current_settings.globalswitches) = []) and
  637. ((asminfo^.id in [as_gas,as_ggas,as_darwin,as_powerpc_xcoff,as_clang,as_solaris_as]));
  638. end;
  639. function TExternalAssembler.CreateNewAsmWriter: TExternalAssemblerOutputFile;
  640. begin
  641. result:=TExternalAssemblerOutputFile.Create(self);
  642. end;
  643. Constructor TExternalAssembler.Create(info: pasminfo; smart: boolean);
  644. begin
  645. CreateWithWriter(info,CreateNewAsmWriter,true,smart);
  646. end;
  647. constructor TExternalAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter,smart: boolean);
  648. begin
  649. inherited Create(info,smart);
  650. fwriter:=wr;
  651. ffreewriter:=freewriter;
  652. if SmartAsm then
  653. begin
  654. path:=FixPath(ChangeFileExt(AsmFileName,target_info.smartext),false);
  655. CreateSmartLinkPath(path);
  656. end;
  657. end;
  658. procedure TExternalAssembler.CreateSmartLinkPath(const s:TPathStr);
  659. procedure DeleteFilesWithExt(const AExt:string);
  660. var
  661. dir : TRawByteSearchRec;
  662. begin
  663. if findfirst(FixPath(s,false)+'*'+AExt,faAnyFile,dir) = 0 then
  664. begin
  665. repeat
  666. DeleteFile(s+source_info.dirsep+dir.name);
  667. until findnext(dir) <> 0;
  668. end;
  669. findclose(dir);
  670. end;
  671. var
  672. hs : TPathStr;
  673. begin
  674. if PathExists(s,false) then
  675. begin
  676. { the path exists, now we clean only all the .o and .s files }
  677. DeleteFilesWithExt(target_info.objext);
  678. DeleteFilesWithExt(target_info.asmext);
  679. end
  680. else
  681. begin
  682. hs:=s;
  683. if hs[length(hs)] in ['/','\'] then
  684. delete(hs,length(hs),1);
  685. {$push} {$I-}
  686. mkdir(hs);
  687. {$pop}
  688. if ioresult<>0 then;
  689. end;
  690. end;
  691. const
  692. lastas : byte=255;
  693. var
  694. LastASBin : TCmdStr;
  695. Function TExternalAssembler.FindAssembler:string;
  696. var
  697. asfound : boolean;
  698. UtilExe : string;
  699. begin
  700. asfound:=false;
  701. if cs_link_on_target in current_settings.globalswitches then
  702. begin
  703. { If linking on target, don't add any path PM }
  704. FindAssembler:=utilsprefix+ChangeFileExt(asminfo^.asmbin,target_info.exeext);
  705. exit;
  706. end
  707. else
  708. UtilExe:=utilsprefix+ChangeFileExt(asminfo^.asmbin,source_info.exeext);
  709. if lastas<>ord(asminfo^.id) then
  710. begin
  711. lastas:=ord(asminfo^.id);
  712. { is an assembler passed ? }
  713. if utilsdirectory<>'' then
  714. asfound:=FindFile(UtilExe,utilsdirectory,false,LastASBin);
  715. if not AsFound then
  716. asfound:=FindExe(UtilExe,false,LastASBin);
  717. if (not asfound) and not(cs_asm_extern in current_settings.globalswitches) then
  718. begin
  719. Message1(exec_e_assembler_not_found,LastASBin);
  720. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  721. end;
  722. if asfound then
  723. Message1(exec_t_using_assembler,LastASBin);
  724. end;
  725. FindAssembler:=LastASBin;
  726. end;
  727. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  728. var
  729. DosExitCode : Integer;
  730. begin
  731. result:=true;
  732. if (cs_asm_extern in current_settings.globalswitches) then
  733. begin
  734. if SmartAsm then
  735. AsmRes.AddAsmCommand(command,para,Name+'('+TosTr(SmartFilesCount)+')')
  736. else
  737. AsmRes.AddAsmCommand(command,para,name);
  738. exit;
  739. end;
  740. try
  741. FlushOutput;
  742. DosExitCode:=RequotedExecuteProcess(command,para);
  743. if DosExitCode<>0
  744. then begin
  745. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  746. result:=false;
  747. end;
  748. except on E:EOSError do
  749. begin
  750. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  751. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  752. result:=false;
  753. end;
  754. end;
  755. end;
  756. Function TExternalAssembler.DoAssemble:boolean;
  757. begin
  758. DoAssemble:=true;
  759. if DoPipe then
  760. exit;
  761. if not(cs_asm_extern in current_settings.globalswitches) then
  762. begin
  763. if SmartAsm then
  764. begin
  765. if (SmartFilesCount<=1) then
  766. Message1(exec_i_assembling_smart,name);
  767. end
  768. else
  769. Message1(exec_i_assembling,name);
  770. end;
  771. if CallAssembler(FindAssembler,MakeCmdLine) then
  772. writer.RemoveAsm
  773. else
  774. begin
  775. DoAssemble:=false;
  776. GenerateError;
  777. end;
  778. end;
  779. function TExternalAssembler.MakeCmdLine: TCmdStr;
  780. function section_high_bound:longint;
  781. var
  782. alt : tasmlisttype;
  783. begin
  784. result:=0;
  785. for alt:=low(tasmlisttype) to high(tasmlisttype) do
  786. result:=result+current_asmdata.asmlists[alt].section_count;
  787. end;
  788. const
  789. min_big_obj_section_count = $7fff;
  790. begin
  791. result:=asminfo^.asmcmd;
  792. { for Xcode 7.x and later }
  793. if MacOSXVersionMin<>'' then
  794. Replace(result,'$DARWINVERSION','-mmacosx-version-min='+MacOSXVersionMin)
  795. else if iPhoneOSVersionMin<>'' then
  796. Replace(result,'$DARWINVERSION','-miphoneos-version-min='+iPhoneOSVersionMin)
  797. else
  798. Replace(result,'$DARWINVERSION','');
  799. {$ifdef arm}
  800. if (target_info.system=system_arm_darwin) then
  801. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]));
  802. {$endif arm}
  803. if (cs_link_on_target in current_settings.globalswitches) then
  804. begin
  805. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  806. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  807. end
  808. else
  809. begin
  810. {$ifdef hasunix}
  811. if DoPipe then
  812. if asminfo^.id<>as_clang then
  813. Replace(result,'$ASM','')
  814. else
  815. Replace(result,'$ASM','-')
  816. else
  817. {$endif}
  818. Replace(result,'$ASM',maybequoted(AsmFileName));
  819. Replace(result,'$OBJ',maybequoted(ObjFileName));
  820. end;
  821. if (cs_create_pic in current_settings.moduleswitches) then
  822. Replace(result,'$PIC','-KPIC')
  823. else
  824. Replace(result,'$PIC','');
  825. if (cs_asm_source in current_settings.globalswitches) then
  826. Replace(result,'$NOWARN','')
  827. else
  828. Replace(result,'$NOWARN','-W');
  829. if target_info.endian=endian_little then
  830. Replace(result,'$ENDIAN','-mlittle')
  831. else
  832. Replace(result,'$ENDIAN','-mbig');
  833. { as we don't keep track of the amount of sections we created we simply
  834. enable Big Obj COFF files always for targets that need them }
  835. if (cs_asm_pre_binutils_2_25 in current_settings.globalswitches) or
  836. not (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) or
  837. (section_high_bound<min_big_obj_section_count) then
  838. Replace(result,'$BIGOBJ','')
  839. else
  840. Replace(result,'$BIGOBJ','-mbig-obj');
  841. Replace(result,'$EXTRAOPT',asmextraopt);
  842. end;
  843. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  844. var
  845. module : tmodule;
  846. begin
  847. { load infile }
  848. if (lastfileinfo.moduleindex<>hp.fileinfo.moduleindex) or
  849. (lastfileinfo.fileindex<>hp.fileinfo.fileindex) then
  850. begin
  851. { in case of a generic the module can be different }
  852. if current_module.unit_index=hp.fileinfo.moduleindex then
  853. module:=current_module
  854. else
  855. module:=get_module(hp.fileinfo.moduleindex);
  856. { during the compilation of the system unit there are cases when
  857. the fileinfo contains just zeros => invalid }
  858. if assigned(module) then
  859. infile:=module.sourcefiles.get_file(hp.fileinfo.fileindex)
  860. else
  861. infile:=nil;
  862. if assigned(infile) then
  863. begin
  864. { open only if needed !! }
  865. if (cs_asm_source in current_settings.globalswitches) then
  866. infile.open;
  867. end;
  868. { avoid unnecessary reopens of the same file !! }
  869. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  870. lastfileinfo.moduleindex:=hp.fileinfo.moduleindex;
  871. { be sure to change line !! }
  872. lastfileinfo.line:=-1;
  873. end;
  874. { write source }
  875. if (cs_asm_source in current_settings.globalswitches) and
  876. assigned(infile) then
  877. begin
  878. if (infile<>lastinfile) then
  879. begin
  880. writer.AsmWriteLn(asminfo^.comment+'['+infile.name+']');
  881. if assigned(lastinfile) then
  882. lastinfile.close;
  883. end;
  884. if (hp.fileinfo.line<>lastfileinfo.line) and
  885. (hp.fileinfo.line<infile.maxlinebuf) then
  886. begin
  887. if (hp.fileinfo.line<>0) and
  888. (infile.linebuf^[hp.fileinfo.line]>=0) then
  889. writer.AsmWriteLn(asminfo^.comment+'['+tostr(hp.fileinfo.line)+'] '+
  890. fixline(infile.GetLineStr(hp.fileinfo.line)));
  891. { set it to a negative value !
  892. to make that is has been read already !! PM }
  893. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  894. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  895. end;
  896. end;
  897. lastfileinfo:=hp.fileinfo;
  898. lastinfile:=infile;
  899. end;
  900. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  901. begin
  902. {$ifdef EXTDEBUG}
  903. if assigned(hp.problem) then
  904. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  905. tostr(hp.tempsize)+' '+hp.problem^)
  906. else
  907. {$endif EXTDEBUG}
  908. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  909. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  910. end;
  911. procedure TExternalAssembler.WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  912. var
  913. pdata: pbyte;
  914. index, step, swapmask, count: longint;
  915. ssingle: single;
  916. ddouble: double;
  917. ccomp: comp;
  918. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  919. eextended: extended;
  920. {$else}
  921. {$ifdef FPC_SOFT_FPUX80}
  922. eextended: floatx80;
  923. {$endif}
  924. {$endif cpuextended}
  925. begin
  926. if do_line then
  927. begin
  928. case tai_realconst(hp).realtyp of
  929. aitrealconst_s32bit:
  930. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  931. aitrealconst_s64bit:
  932. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  933. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  934. { can't write full 80 bit floating point constants yet on non-x86 }
  935. aitrealconst_s80bit:
  936. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  937. {$else}
  938. {$ifdef FPC_SOFT_FPUX80}
  939. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  940. aitrealconst_s80bit:
  941. begin
  942. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  943. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  944. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  945. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  946. else
  947. internalerror(2017091901);
  948. end;
  949. {$pop}
  950. {$endif}
  951. {$endif cpuextended}
  952. aitrealconst_s64comp:
  953. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  954. else
  955. internalerror(2014050604);
  956. end;
  957. end;
  958. writer.AsmWrite(dbdir);
  959. { generic float writing code: get start address of value, then write
  960. byte by byte. Can't use fields directly, because e.g ts64comp is
  961. defined as extended on x86 }
  962. case tai_realconst(hp).realtyp of
  963. aitrealconst_s32bit:
  964. begin
  965. ssingle:=single(tai_realconst(hp).value.s32val);
  966. pdata:=@ssingle;
  967. end;
  968. aitrealconst_s64bit:
  969. begin
  970. ddouble:=double(tai_realconst(hp).value.s64val);
  971. pdata:=@ddouble;
  972. end;
  973. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  974. { can't write full 80 bit floating point constants yet on non-x86 }
  975. aitrealconst_s80bit:
  976. begin
  977. eextended:=extended(tai_realconst(hp).value.s80val);
  978. pdata:=@eextended;
  979. end;
  980. {$else}
  981. {$ifdef FPC_SOFT_FPUX80}
  982. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  983. aitrealconst_s80bit:
  984. begin
  985. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  986. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  987. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  988. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  989. else
  990. internalerror(2017091901);
  991. pdata:=@eextended;
  992. end;
  993. {$pop}
  994. {$endif}
  995. {$endif cpuextended}
  996. aitrealconst_s64comp:
  997. begin
  998. ccomp:=comp(tai_realconst(hp).value.s64compval);
  999. pdata:=@ccomp;
  1000. end;
  1001. else
  1002. internalerror(2014051001);
  1003. end;
  1004. count:=tai_realconst(hp).datasize;
  1005. { write bytes in inverse order if source and target endianess don't
  1006. match }
  1007. if source_info.endian<>target_info.endian then
  1008. begin
  1009. { go from back to front }
  1010. index:=count-1;
  1011. step:=-1;
  1012. end
  1013. else
  1014. begin
  1015. index:=0;
  1016. step:=1;
  1017. end;
  1018. {$ifdef ARM}
  1019. { ARM-specific: low and high dwords of a double may be swapped }
  1020. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  1021. begin
  1022. { only supported for double }
  1023. if tai_realconst(hp).datasize<>8 then
  1024. internalerror(2014050605);
  1025. { switch bit of the index so that the words are written in
  1026. the opposite order }
  1027. swapmask:=4;
  1028. end
  1029. else
  1030. {$endif ARM}
  1031. swapmask:=0;
  1032. repeat
  1033. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  1034. inc(index,step);
  1035. dec(count);
  1036. if count<>0 then
  1037. writer.AsmWrite(',');
  1038. until count=0;
  1039. { padding }
  1040. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  1041. writer.AsmWrite(',0');
  1042. writer.AsmLn;
  1043. end;
  1044. procedure TExternalAssembler.WriteTree(p:TAsmList);
  1045. begin
  1046. end;
  1047. procedure TExternalAssembler.WriteAsmList;
  1048. begin
  1049. end;
  1050. procedure TExternalAssembler.MakeObject;
  1051. begin
  1052. writer.AsmCreate(cut_normal);
  1053. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  1054. lastfileinfo.line := -1;
  1055. lastinfile := nil;
  1056. lastsectype := sec_none;
  1057. WriteAsmList;
  1058. writer.AsmClose;
  1059. if not(writer.ioerror) then
  1060. DoAssemble;
  1061. end;
  1062. destructor TExternalAssembler.Destroy;
  1063. begin
  1064. if ffreewriter then
  1065. writer.Free;
  1066. inherited;
  1067. end;
  1068. {*****************************************************************************
  1069. TInternalAssembler
  1070. *****************************************************************************}
  1071. constructor TInternalAssembler.Create(info: pasminfo; smart: boolean);
  1072. begin
  1073. inherited;
  1074. ObjOutput:=nil;
  1075. ObjData:=nil;
  1076. SmartAsm:=smart;
  1077. end;
  1078. destructor TInternalAssembler.destroy;
  1079. begin
  1080. if assigned(ObjData) then
  1081. ObjData.free;
  1082. if assigned(ObjOutput) then
  1083. ObjOutput.free;
  1084. end;
  1085. procedure TInternalAssembler.WriteStab(p:pchar);
  1086. function consumecomma(var p:pchar):boolean;
  1087. begin
  1088. while (p^=' ') do
  1089. inc(p);
  1090. result:=(p^=',');
  1091. inc(p);
  1092. end;
  1093. function consumenumber(var p:pchar;out value:longint):boolean;
  1094. var
  1095. hs : string;
  1096. len,
  1097. code : integer;
  1098. begin
  1099. value:=0;
  1100. while (p^=' ') do
  1101. inc(p);
  1102. len:=0;
  1103. while (p^ in ['0'..'9']) do
  1104. begin
  1105. inc(len);
  1106. hs[len]:=p^;
  1107. inc(p);
  1108. end;
  1109. if len>0 then
  1110. begin
  1111. hs[0]:=chr(len);
  1112. val(hs,value,code);
  1113. end
  1114. else
  1115. code:=-1;
  1116. result:=(code=0);
  1117. end;
  1118. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  1119. var
  1120. hs : string;
  1121. len,
  1122. code : integer;
  1123. pstart : pchar;
  1124. sym : tobjsymbol;
  1125. exprvalue : longint;
  1126. gotmin,
  1127. have_first_symbol,
  1128. have_second_symbol,
  1129. dosub : boolean;
  1130. begin
  1131. result:=false;
  1132. value:=0;
  1133. relocsym:=nil;
  1134. gotmin:=false;
  1135. have_first_symbol:=false;
  1136. have_second_symbol:=false;
  1137. repeat
  1138. dosub:=false;
  1139. exprvalue:=0;
  1140. if gotmin then
  1141. begin
  1142. dosub:=true;
  1143. gotmin:=false;
  1144. end;
  1145. while (p^=' ') do
  1146. inc(p);
  1147. case p^ of
  1148. #0 :
  1149. break;
  1150. ' ' :
  1151. inc(p);
  1152. '0'..'9' :
  1153. begin
  1154. len:=0;
  1155. while (p^ in ['0'..'9']) do
  1156. begin
  1157. inc(len);
  1158. hs[len]:=p^;
  1159. inc(p);
  1160. end;
  1161. hs[0]:=chr(len);
  1162. val(hs,exprvalue,code);
  1163. if code<>0 then
  1164. internalerror(200702251);
  1165. end;
  1166. '.','_',
  1167. 'A'..'Z',
  1168. 'a'..'z' :
  1169. begin
  1170. pstart:=p;
  1171. while not(p^ in [#0,' ','-','+']) do
  1172. inc(p);
  1173. len:=p-pstart;
  1174. if len>255 then
  1175. internalerror(200509187);
  1176. move(pstart^,hs[1],len);
  1177. hs[0]:=chr(len);
  1178. sym:=objdata.symbolref(hs);
  1179. { Second symbol? }
  1180. if assigned(relocsym) then
  1181. begin
  1182. if have_second_symbol then
  1183. internalerror(2007032201);
  1184. have_second_symbol:=true;
  1185. if not have_first_symbol then
  1186. internalerror(2007032202);
  1187. { second symbol should substracted to first }
  1188. if not dosub then
  1189. internalerror(2007032203);
  1190. if (relocsym.objsection<>sym.objsection) then
  1191. internalerror(2005091810);
  1192. exprvalue:=relocsym.address-sym.address;
  1193. relocsym:=nil;
  1194. dosub:=false;
  1195. end
  1196. else
  1197. begin
  1198. relocsym:=sym;
  1199. if assigned(sym.objsection) then
  1200. begin
  1201. { first symbol should be + }
  1202. if not have_first_symbol and dosub then
  1203. internalerror(2007032204);
  1204. have_first_symbol:=true;
  1205. end;
  1206. end;
  1207. end;
  1208. '+' :
  1209. begin
  1210. { nothing, by default addition is done }
  1211. inc(p);
  1212. end;
  1213. '-' :
  1214. begin
  1215. gotmin:=true;
  1216. inc(p);
  1217. end;
  1218. else
  1219. internalerror(200509189);
  1220. end;
  1221. if dosub then
  1222. dec(value,exprvalue)
  1223. else
  1224. inc(value,exprvalue);
  1225. until false;
  1226. result:=true;
  1227. end;
  1228. var
  1229. stabstrlen,
  1230. ofs,
  1231. nline,
  1232. nidx,
  1233. nother,
  1234. i : longint;
  1235. stab : TObjStabEntry;
  1236. relocsym : TObjSymbol;
  1237. pstr,
  1238. pcurr,
  1239. pendquote : pchar;
  1240. oldsec : TObjSection;
  1241. begin
  1242. pcurr:=nil;
  1243. pstr:=nil;
  1244. pendquote:=nil;
  1245. relocsym:=nil;
  1246. ofs:=0;
  1247. { Parse string part }
  1248. if (p[0]='"') then
  1249. begin
  1250. pstr:=@p[1];
  1251. { Ignore \" inside the string }
  1252. i:=1;
  1253. while not((p[i]='"') and (p[i-1]<>'\')) and
  1254. (p[i]<>#0) do
  1255. inc(i);
  1256. pendquote:=@p[i];
  1257. pendquote^:=#0;
  1258. pcurr:=@p[i+1];
  1259. if not consumecomma(pcurr) then
  1260. internalerror(200509181);
  1261. end
  1262. else
  1263. pcurr:=p;
  1264. { When in pass 1 then only alloc and leave }
  1265. if ObjData.currpass=1 then
  1266. begin
  1267. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  1268. if assigned(pstr) and (pstr[0]<>#0) then
  1269. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  1270. end
  1271. else
  1272. begin
  1273. { Stabs format: nidx,nother,nline[,offset] }
  1274. if not consumenumber(pcurr,nidx) then
  1275. internalerror(200509182);
  1276. if not consumecomma(pcurr) then
  1277. internalerror(200509183);
  1278. if not consumenumber(pcurr,nother) then
  1279. internalerror(200509184);
  1280. if not consumecomma(pcurr) then
  1281. internalerror(200509185);
  1282. if not consumenumber(pcurr,nline) then
  1283. internalerror(200509186);
  1284. if consumecomma(pcurr) then
  1285. consumeoffset(pcurr,relocsym,ofs);
  1286. { Generate stab entry }
  1287. if assigned(pstr) and (pstr[0]<>#0) then
  1288. begin
  1289. stabstrlen:=strlen(pstr);
  1290. {$ifdef optimizestabs}
  1291. StabStrEntry:=nil;
  1292. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  1293. begin
  1294. hs:=strpas(pstr);
  1295. StabstrEntry:=StabStrDict.Find(hs);
  1296. if not assigned(StabstrEntry) then
  1297. begin
  1298. StabstrEntry:=TStabStrEntry.Create(hs);
  1299. StabstrEntry:=StabStrSec.Size;
  1300. StabStrDict.Insert(StabstrEntry);
  1301. { generate new stab }
  1302. StabstrEntry:=nil;
  1303. end;
  1304. end;
  1305. if assigned(StabstrEntry) then
  1306. stab.strpos:=StabstrEntry.strpos
  1307. else
  1308. {$endif optimizestabs}
  1309. begin
  1310. stab.strpos:=ObjData.StabStrSec.Size;
  1311. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  1312. end;
  1313. end
  1314. else
  1315. stab.strpos:=0;
  1316. stab.ntype:=byte(nidx);
  1317. stab.ndesc:=word(nline);
  1318. stab.nother:=byte(nother);
  1319. stab.nvalue:=ofs;
  1320. { Write the stab first without the value field. Then
  1321. write a the value field with relocation }
  1322. oldsec:=ObjData.CurrObjSec;
  1323. ObjData.SetSection(ObjData.StabsSec);
  1324. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  1325. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  1326. ObjData.setsection(oldsec);
  1327. end;
  1328. if assigned(pendquote) then
  1329. pendquote^:='"';
  1330. end;
  1331. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  1332. begin
  1333. { maybe end of list }
  1334. while not assigned(hp) do
  1335. begin
  1336. if currlistidx<lists then
  1337. begin
  1338. inc(currlistidx);
  1339. currlist:=list[currlistidx];
  1340. hp:=Tai(currList.first);
  1341. end
  1342. else
  1343. begin
  1344. MaybeNextList:=false;
  1345. exit;
  1346. end;
  1347. end;
  1348. MaybeNextList:=true;
  1349. end;
  1350. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  1351. var
  1352. objsym : TObjSymbol;
  1353. indsym : TObjSymbol;
  1354. begin
  1355. Result:=
  1356. Assigned(hp) and
  1357. (hp.typ=ait_symbol);
  1358. if not Result then
  1359. Exit;
  1360. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1361. objsym.size:=0;
  1362. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1363. if not Assigned(indsym) then
  1364. begin
  1365. { it's possible that indirect symbol is not present in the list,
  1366. so we must create it as undefined }
  1367. indsym:=ObjData.CObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1368. indsym.typ:=AT_NONE;
  1369. indsym.bind:=AB_NONE;
  1370. end;
  1371. objsym.indsymbol:=indsym;
  1372. Result:=true;
  1373. end;
  1374. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1375. var
  1376. objsym,
  1377. objsymend : TObjSymbol;
  1378. cpu: tcputype;
  1379. begin
  1380. while assigned(hp) do
  1381. begin
  1382. case hp.typ of
  1383. ait_align :
  1384. begin
  1385. if tai_align_abstract(hp).aligntype>1 then
  1386. begin
  1387. { always use the maximum fillsize in this pass to avoid possible
  1388. short jumps to become out of range }
  1389. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1390. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1391. { may need to increase alignment of section }
  1392. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1393. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1394. end
  1395. else
  1396. Tai_align_abstract(hp).fillsize:=0;
  1397. end;
  1398. ait_datablock :
  1399. begin
  1400. {$ifdef USE_COMM_IN_BSS}
  1401. if writingpackages and
  1402. Tai_datablock(hp).is_global then
  1403. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1404. else
  1405. {$endif USE_COMM_IN_BSS}
  1406. begin
  1407. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1408. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1409. ObjData.alloc(Tai_datablock(hp).size);
  1410. end;
  1411. end;
  1412. ait_realconst:
  1413. ObjData.alloc(tai_realconst(hp).savesize);
  1414. ait_const:
  1415. begin
  1416. { if symbols are provided we can calculate the value for relative symbols.
  1417. This is required for length calculation of leb128 constants }
  1418. if assigned(tai_const(hp).sym) then
  1419. begin
  1420. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1421. { objsym already defined and there is endsym? }
  1422. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1423. begin
  1424. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1425. { objsymend already defined? }
  1426. if assigned(objsymend.objsection) then
  1427. begin
  1428. if objsymend.objsection<>objsym.objsection then
  1429. begin
  1430. { leb128 relative constants are not relocatable, but other types are,
  1431. given that objsym belongs to the current section. }
  1432. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1433. (objsym.objsection<>ObjData.CurrObjSec) then
  1434. InternalError(200404124);
  1435. end
  1436. else
  1437. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1438. end;
  1439. end;
  1440. end;
  1441. ObjData.alloc(tai_const(hp).size);
  1442. end;
  1443. ait_directive:
  1444. begin
  1445. case tai_directive(hp).directive of
  1446. asd_indirect_symbol:
  1447. { handled in TreePass1 }
  1448. ;
  1449. asd_lazy_reference:
  1450. begin
  1451. if tai_directive(hp).name='' then
  1452. Internalerror(2009112101);
  1453. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1454. objsym.bind:=AB_LAZY;
  1455. end;
  1456. asd_reference:
  1457. { ignore for now, but should be added}
  1458. ;
  1459. asd_cpu:
  1460. begin
  1461. ObjData.CPUType:=cpu_none;
  1462. for cpu:=low(tcputype) to high(tcputype) do
  1463. if cputypestr[cpu]=tai_directive(hp).name then
  1464. begin
  1465. ObjData.CPUType:=cpu;
  1466. break;
  1467. end;
  1468. end;
  1469. {$ifdef OMFOBJSUPPORT}
  1470. asd_omf_linnum_line:
  1471. { ignore for now, but should be added}
  1472. ;
  1473. {$endif OMFOBJSUPPORT}
  1474. {$ifdef ARM}
  1475. asd_thumb_func:
  1476. ObjData.ThumbFunc:=true;
  1477. asd_code:
  1478. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1479. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1480. {$endif ARM}
  1481. else
  1482. internalerror(2010011101);
  1483. end;
  1484. end;
  1485. ait_section:
  1486. begin
  1487. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1488. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1489. end;
  1490. ait_symbol :
  1491. begin
  1492. { needs extra support in the internal assembler }
  1493. { the value is just ignored }
  1494. {if tai_symbol(hp).has_value then
  1495. internalerror(2009090804); ;}
  1496. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1497. end;
  1498. ait_label :
  1499. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1500. ait_string :
  1501. ObjData.alloc(Tai_string(hp).len);
  1502. ait_instruction :
  1503. begin
  1504. { reset instructions which could change in pass 2 }
  1505. Taicpu(hp).resetpass2;
  1506. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1507. end;
  1508. ait_cutobject :
  1509. if SmartAsm then
  1510. break;
  1511. end;
  1512. hp:=Tai(hp.next);
  1513. end;
  1514. TreePass0:=hp;
  1515. end;
  1516. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1517. var
  1518. objsym,
  1519. objsymend : TObjSymbol;
  1520. cpu: tcputype;
  1521. begin
  1522. while assigned(hp) do
  1523. begin
  1524. case hp.typ of
  1525. ait_align :
  1526. begin
  1527. if tai_align_abstract(hp).aligntype>1 then
  1528. begin
  1529. { here we must determine the fillsize which is used in pass2 }
  1530. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1531. ObjData.CurrObjSec.Size;
  1532. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1533. end;
  1534. end;
  1535. ait_datablock :
  1536. begin
  1537. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1538. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1539. Message(asmw_e_alloc_data_only_in_bss);
  1540. {$ifdef USE_COMM_IN_BSS}
  1541. if writingpackages and
  1542. Tai_datablock(hp).is_global then
  1543. begin
  1544. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1545. objsym.size:=Tai_datablock(hp).size;
  1546. objsym.bind:=AB_COMMON;
  1547. objsym.alignment:=needtowritealignmentalsoforELF;
  1548. end
  1549. else
  1550. {$endif USE_COMM_IN_BSS}
  1551. begin
  1552. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1553. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1554. objsym.size:=Tai_datablock(hp).size;
  1555. ObjData.alloc(Tai_datablock(hp).size);
  1556. end;
  1557. end;
  1558. ait_realconst:
  1559. ObjData.alloc(tai_realconst(hp).savesize);
  1560. ait_const:
  1561. begin
  1562. { Recalculate relative symbols }
  1563. if assigned(tai_const(hp).sym) and
  1564. assigned(tai_const(hp).endsym) then
  1565. begin
  1566. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1567. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1568. if objsymend.objsection<>objsym.objsection then
  1569. begin
  1570. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1571. (objsym.objsection<>ObjData.CurrObjSec) then
  1572. internalerror(200905042);
  1573. end
  1574. else
  1575. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1576. end;
  1577. ObjData.alloc(tai_const(hp).size);
  1578. end;
  1579. ait_section:
  1580. begin
  1581. { use cached value }
  1582. ObjData.setsection(Tai_section(hp).sec);
  1583. end;
  1584. ait_stab :
  1585. begin
  1586. if assigned(Tai_stab(hp).str) then
  1587. WriteStab(Tai_stab(hp).str);
  1588. end;
  1589. ait_symbol :
  1590. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1591. ait_symbol_end :
  1592. begin
  1593. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1594. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1595. end;
  1596. ait_label :
  1597. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1598. ait_string :
  1599. ObjData.alloc(Tai_string(hp).len);
  1600. ait_instruction :
  1601. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1602. ait_cutobject :
  1603. if SmartAsm then
  1604. break;
  1605. ait_directive :
  1606. begin
  1607. case tai_directive(hp).directive of
  1608. asd_indirect_symbol:
  1609. if tai_directive(hp).name='' then
  1610. Internalerror(2009101103)
  1611. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1612. Internalerror(2009101102);
  1613. asd_lazy_reference:
  1614. { handled in TreePass0 }
  1615. ;
  1616. asd_reference:
  1617. { ignore for now, but should be added}
  1618. ;
  1619. asd_thumb_func:
  1620. { ignore for now, but should be added}
  1621. ;
  1622. asd_code:
  1623. { ignore for now, but should be added}
  1624. ;
  1625. {$ifdef OMFOBJSUPPORT}
  1626. asd_omf_linnum_line:
  1627. { ignore for now, but should be added}
  1628. ;
  1629. {$endif OMFOBJSUPPORT}
  1630. asd_cpu:
  1631. begin
  1632. ObjData.CPUType:=cpu_none;
  1633. for cpu:=low(tcputype) to high(tcputype) do
  1634. if cputypestr[cpu]=tai_directive(hp).name then
  1635. begin
  1636. ObjData.CPUType:=cpu;
  1637. break;
  1638. end;
  1639. end;
  1640. else
  1641. internalerror(2010011102);
  1642. end;
  1643. end;
  1644. end;
  1645. hp:=Tai(hp.next);
  1646. end;
  1647. TreePass1:=hp;
  1648. end;
  1649. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1650. var
  1651. fillbuffer : tfillbuffer;
  1652. leblen : byte;
  1653. lebbuf : array[0..63] of byte;
  1654. objsym,
  1655. ref,
  1656. objsymend : TObjSymbol;
  1657. zerobuf : array[0..63] of byte;
  1658. relative_reloc: boolean;
  1659. pdata : pointer;
  1660. ssingle : single;
  1661. ddouble : double;
  1662. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1663. eextended : extended;
  1664. {$else}
  1665. {$ifdef FPC_SOFT_FPUX80}
  1666. eextended : floatx80;
  1667. {$endif}
  1668. {$endif}
  1669. ccomp : comp;
  1670. tmp : word;
  1671. cpu: tcputype;
  1672. begin
  1673. fillchar(zerobuf,sizeof(zerobuf),0);
  1674. fillchar(objsym,sizeof(objsym),0);
  1675. fillchar(objsymend,sizeof(objsymend),0);
  1676. { main loop }
  1677. while assigned(hp) do
  1678. begin
  1679. case hp.typ of
  1680. ait_align :
  1681. begin
  1682. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1683. InternalError(2012072301);
  1684. if oso_data in ObjData.CurrObjSec.secoptions then
  1685. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1686. Tai_align_abstract(hp).fillsize)
  1687. else
  1688. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1689. end;
  1690. ait_section :
  1691. begin
  1692. { use cached value }
  1693. ObjData.setsection(Tai_section(hp).sec);
  1694. end;
  1695. ait_symbol :
  1696. begin
  1697. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1698. end;
  1699. ait_symbol_end :
  1700. begin
  1701. { recalculate size, as some preceding instructions
  1702. could have been changed to smaller size }
  1703. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1704. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1705. end;
  1706. ait_datablock :
  1707. begin
  1708. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1709. {$ifdef USE_COMM_IN_BSS}
  1710. if not(writingpackages and
  1711. Tai_datablock(hp).is_global) then
  1712. {$endif USE_COMM_IN_BSS}
  1713. begin
  1714. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1715. ObjData.alloc(Tai_datablock(hp).size);
  1716. end;
  1717. end;
  1718. ait_realconst:
  1719. begin
  1720. case tai_realconst(hp).realtyp of
  1721. aitrealconst_s32bit:
  1722. begin
  1723. ssingle:=single(tai_realconst(hp).value.s32val);
  1724. pdata:=@ssingle;
  1725. end;
  1726. aitrealconst_s64bit:
  1727. begin
  1728. ddouble:=double(tai_realconst(hp).value.s64val);
  1729. pdata:=@ddouble;
  1730. end;
  1731. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1732. { can't write full 80 bit floating point constants yet on non-x86 }
  1733. aitrealconst_s80bit:
  1734. begin
  1735. eextended:=extended(tai_realconst(hp).value.s80val);
  1736. pdata:=@eextended;
  1737. end;
  1738. {$else}
  1739. {$ifdef FPC_SOFT_FPUX80}
  1740. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1741. aitrealconst_s80bit:
  1742. begin
  1743. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1744. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  1745. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1746. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  1747. else
  1748. internalerror(2017091901);
  1749. pdata:=@eextended;
  1750. end;
  1751. {$pop}
  1752. {$endif}
  1753. {$endif cpuextended}
  1754. aitrealconst_s64comp:
  1755. begin
  1756. ccomp:=comp(tai_realconst(hp).value.s64compval);
  1757. pdata:=@ccomp;
  1758. end;
  1759. else
  1760. internalerror(2015030501);
  1761. end;
  1762. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  1763. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  1764. end;
  1765. ait_string :
  1766. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1767. ait_const :
  1768. begin
  1769. { Recalculate relative symbols, addresses of forward references
  1770. can be changed in treepass1 }
  1771. relative_reloc:=false;
  1772. if assigned(tai_const(hp).sym) and
  1773. assigned(tai_const(hp).endsym) then
  1774. begin
  1775. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1776. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1777. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  1778. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1779. end;
  1780. case tai_const(hp).consttype of
  1781. aitconst_64bit,
  1782. aitconst_32bit,
  1783. aitconst_16bit,
  1784. aitconst_64bit_unaligned,
  1785. aitconst_32bit_unaligned,
  1786. aitconst_16bit_unaligned,
  1787. aitconst_8bit :
  1788. begin
  1789. if assigned(tai_const(hp).sym) and
  1790. not assigned(tai_const(hp).endsym) then
  1791. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1792. else if relative_reloc then
  1793. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  1794. else
  1795. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1796. end;
  1797. aitconst_rva_symbol :
  1798. begin
  1799. { PE32+? }
  1800. if target_info.system=system_x86_64_win64 then
  1801. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1802. else
  1803. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1804. end;
  1805. aitconst_secrel32_symbol :
  1806. begin
  1807. { Required for DWARF2 support under Windows }
  1808. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1809. end;
  1810. {$ifdef i8086}
  1811. aitconst_farptr :
  1812. if assigned(tai_const(hp).sym) and
  1813. not assigned(tai_const(hp).endsym) then
  1814. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  1815. else if relative_reloc then
  1816. internalerror(2015040601)
  1817. else
  1818. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1819. aitconst_seg:
  1820. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  1821. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  1822. else
  1823. internalerror(2015110502);
  1824. aitconst_dgroup:
  1825. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  1826. aitconst_fardataseg:
  1827. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  1828. {$endif i8086}
  1829. {$ifdef arm}
  1830. aitconst_got:
  1831. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  1832. {$endif arm}
  1833. aitconst_gotoff_symbol:
  1834. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  1835. aitconst_uleb128bit,
  1836. aitconst_sleb128bit :
  1837. begin
  1838. if tai_const(hp).consttype=aitconst_uleb128bit then
  1839. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1840. else
  1841. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1842. if leblen<>tai_const(hp).size then
  1843. internalerror(200709271);
  1844. ObjData.writebytes(lebbuf,leblen);
  1845. end;
  1846. aitconst_darwin_dwarf_delta32,
  1847. aitconst_darwin_dwarf_delta64:
  1848. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1849. aitconst_half16bit,
  1850. aitconst_gs:
  1851. begin
  1852. tmp:=Tai_const(hp).value div 2;
  1853. ObjData.writebytes(tmp,2);
  1854. end;
  1855. else
  1856. internalerror(200603254);
  1857. end;
  1858. end;
  1859. ait_label :
  1860. begin
  1861. { exporting shouldn't be necessary as labels are local,
  1862. but it's better to be on the safe side (PFV) }
  1863. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1864. end;
  1865. ait_instruction :
  1866. Taicpu(hp).Pass2(ObjData);
  1867. ait_stab :
  1868. WriteStab(Tai_stab(hp).str);
  1869. ait_function_name,
  1870. ait_force_line : ;
  1871. ait_cutobject :
  1872. if SmartAsm then
  1873. break;
  1874. ait_directive :
  1875. begin
  1876. case tai_directive(hp).directive of
  1877. asd_weak_definition,
  1878. asd_weak_reference:
  1879. begin
  1880. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1881. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  1882. objsym.bind:=AB_WEAK_EXTERNAL
  1883. else
  1884. { TODO: should become a weak definition; for now, do
  1885. the same as what was done for ait_weak }
  1886. objsym.bind:=AB_WEAK_EXTERNAL;
  1887. end;
  1888. asd_cpu:
  1889. begin
  1890. ObjData.CPUType:=cpu_none;
  1891. for cpu:=low(tcputype) to high(tcputype) do
  1892. if cputypestr[cpu]=tai_directive(hp).name then
  1893. begin
  1894. ObjData.CPUType:=cpu;
  1895. break;
  1896. end;
  1897. end;
  1898. {$ifdef OMFOBJSUPPORT}
  1899. asd_omf_linnum_line:
  1900. begin
  1901. TOmfObjSection(ObjData.CurrObjSec).LinNumEntries.Add(
  1902. TOmfSubRecord_LINNUM_MsLink_Entry.Create(
  1903. strtoint(tai_directive(hp).name),
  1904. ObjData.CurrObjSec.Size
  1905. ));
  1906. end;
  1907. {$endif OMFOBJSUPPORT}
  1908. end
  1909. end;
  1910. ait_symbolpair:
  1911. begin
  1912. if tai_symbolpair(hp).kind=spk_set then
  1913. begin
  1914. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  1915. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  1916. objsym.offset:=ref.offset;
  1917. objsym.objsection:=ref.objsection;
  1918. {$ifdef arm}
  1919. objsym.ThumbFunc:=ref.ThumbFunc;
  1920. {$endif arm}
  1921. end;
  1922. end;
  1923. {$ifndef DISABLE_WIN64_SEH}
  1924. ait_seh_directive :
  1925. tai_seh_directive(hp).generate_code(objdata);
  1926. {$endif DISABLE_WIN64_SEH}
  1927. end;
  1928. hp:=Tai(hp.next);
  1929. end;
  1930. TreePass2:=hp;
  1931. end;
  1932. procedure TInternalAssembler.writetree;
  1933. label
  1934. doexit;
  1935. var
  1936. hp : Tai;
  1937. ObjWriter : TObjectWriter;
  1938. begin
  1939. ObjWriter:=TObjectwriter.create;
  1940. ObjOutput:=CObjOutput.Create(ObjWriter);
  1941. ObjData:=ObjOutput.newObjData(ObjFileName);
  1942. { Pass 0 }
  1943. ObjData.currpass:=0;
  1944. ObjData.createsection(sec_code);
  1945. ObjData.beforealloc;
  1946. { start with list 1 }
  1947. currlistidx:=1;
  1948. currlist:=list[currlistidx];
  1949. hp:=Tai(currList.first);
  1950. while assigned(hp) do
  1951. begin
  1952. hp:=TreePass0(hp);
  1953. MaybeNextList(hp);
  1954. end;
  1955. ObjData.afteralloc;
  1956. { leave if errors have occurred }
  1957. if errorcount>0 then
  1958. goto doexit;
  1959. { Pass 1 }
  1960. ObjData.currpass:=1;
  1961. ObjData.resetsections;
  1962. ObjData.beforealloc;
  1963. ObjData.createsection(sec_code);
  1964. { start with list 1 }
  1965. currlistidx:=1;
  1966. currlist:=list[currlistidx];
  1967. hp:=Tai(currList.first);
  1968. while assigned(hp) do
  1969. begin
  1970. hp:=TreePass1(hp);
  1971. MaybeNextList(hp);
  1972. end;
  1973. ObjData.createsection(sec_code);
  1974. ObjData.afteralloc;
  1975. { leave if errors have occurred }
  1976. if errorcount>0 then
  1977. goto doexit;
  1978. { Pass 2 }
  1979. ObjData.currpass:=2;
  1980. ObjData.resetsections;
  1981. ObjData.beforewrite;
  1982. ObjData.createsection(sec_code);
  1983. { start with list 1 }
  1984. currlistidx:=1;
  1985. currlist:=list[currlistidx];
  1986. hp:=Tai(currList.first);
  1987. while assigned(hp) do
  1988. begin
  1989. hp:=TreePass2(hp);
  1990. MaybeNextList(hp);
  1991. end;
  1992. ObjData.createsection(sec_code);
  1993. ObjData.afterwrite;
  1994. { don't write the .o file if errors have occurred }
  1995. if errorcount=0 then
  1996. begin
  1997. { write objectfile }
  1998. ObjOutput.startobjectfile(ObjFileName);
  1999. ObjOutput.writeobjectfile(ObjData);
  2000. end;
  2001. doexit:
  2002. { Cleanup }
  2003. ObjData.free;
  2004. ObjData:=nil;
  2005. ObjWriter.free;
  2006. end;
  2007. procedure TInternalAssembler.writetreesmart;
  2008. var
  2009. hp : Tai;
  2010. startsectype : TAsmSectiontype;
  2011. place: tcutplace;
  2012. ObjWriter : TObjectWriter;
  2013. startsecname: String;
  2014. startsecorder: TAsmSectionOrder;
  2015. begin
  2016. if not(cs_asm_leave in current_settings.globalswitches) and
  2017. not(af_needar in asminfo^.flags) then
  2018. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  2019. else
  2020. ObjWriter:=TObjectwriter.create;
  2021. NextSmartName(cut_normal);
  2022. ObjOutput:=CObjOutput.Create(ObjWriter);
  2023. startsectype:=sec_none;
  2024. startsecname:='';
  2025. startsecorder:=secorder_default;
  2026. { start with list 1 }
  2027. currlistidx:=1;
  2028. currlist:=list[currlistidx];
  2029. hp:=Tai(currList.first);
  2030. while assigned(hp) do
  2031. begin
  2032. ObjData:=ObjOutput.newObjData(ObjFileName);
  2033. { Pass 0 }
  2034. ObjData.currpass:=0;
  2035. ObjData.resetsections;
  2036. ObjData.beforealloc;
  2037. if startsectype<>sec_none then
  2038. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2039. TreePass0(hp);
  2040. ObjData.afteralloc;
  2041. { leave if errors have occurred }
  2042. if errorcount>0 then
  2043. break;
  2044. { Pass 1 }
  2045. ObjData.currpass:=1;
  2046. ObjData.resetsections;
  2047. ObjData.beforealloc;
  2048. if startsectype<>sec_none then
  2049. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2050. TreePass1(hp);
  2051. ObjData.afteralloc;
  2052. { leave if errors have occurred }
  2053. if errorcount>0 then
  2054. break;
  2055. { Pass 2 }
  2056. ObjData.currpass:=2;
  2057. ObjOutput.startobjectfile(ObjFileName);
  2058. ObjData.resetsections;
  2059. ObjData.beforewrite;
  2060. if startsectype<>sec_none then
  2061. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2062. hp:=TreePass2(hp);
  2063. ObjData.afterwrite;
  2064. { leave if errors have occurred }
  2065. if errorcount>0 then
  2066. break;
  2067. { write the current objectfile }
  2068. ObjOutput.writeobjectfile(ObjData);
  2069. ObjData.free;
  2070. ObjData:=nil;
  2071. { end of lists? }
  2072. if not MaybeNextList(hp) then
  2073. break;
  2074. { we will start a new objectfile so reset everything }
  2075. { The place can still change in the next while loop, so don't init }
  2076. { the writer yet (JM) }
  2077. if (hp.typ=ait_cutobject) then
  2078. place := Tai_cutobject(hp).place
  2079. else
  2080. place := cut_normal;
  2081. { avoid empty files }
  2082. startsectype:=sec_none;
  2083. startsecname:='';
  2084. startsecorder:=secorder_default;
  2085. while assigned(hp) and
  2086. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2087. begin
  2088. if Tai(hp).typ=ait_section then
  2089. begin
  2090. startsectype:=Tai_section(hp).sectype;
  2091. startsecname:=Tai_section(hp).name^;
  2092. startsecorder:=Tai_section(hp).secorder;
  2093. end;
  2094. if (Tai(hp).typ=ait_cutobject) then
  2095. place:=Tai_cutobject(hp).place;
  2096. hp:=Tai(hp.next);
  2097. end;
  2098. if not MaybeNextList(hp) then
  2099. break;
  2100. { start next objectfile }
  2101. NextSmartName(place);
  2102. end;
  2103. ObjData.free;
  2104. ObjData:=nil;
  2105. ObjWriter.free;
  2106. end;
  2107. procedure TInternalAssembler.MakeObject;
  2108. var to_do:set of TasmlistType;
  2109. i:TasmlistType;
  2110. procedure addlist(p:TAsmList);
  2111. begin
  2112. inc(lists);
  2113. list[lists]:=p;
  2114. end;
  2115. begin
  2116. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2117. if usedeffileforexports then
  2118. exclude(to_do,al_exports);
  2119. if not(tf_section_threadvars in target_info.flags) then
  2120. exclude(to_do,al_threadvars);
  2121. for i:=low(TasmlistType) to high(TasmlistType) do
  2122. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2123. (not current_asmdata.asmlists[i].empty) then
  2124. addlist(current_asmdata.asmlists[i]);
  2125. if SmartAsm then
  2126. writetreesmart
  2127. else
  2128. writetree;
  2129. end;
  2130. {*****************************************************************************
  2131. Generate Assembler Files Main Procedure
  2132. *****************************************************************************}
  2133. Procedure GenerateAsm(smart:boolean);
  2134. var
  2135. a : TAssembler;
  2136. begin
  2137. if not assigned(CAssembler[target_asm.id]) then
  2138. Message(asmw_f_assembler_output_not_supported);
  2139. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2140. a.MakeObject;
  2141. a.Free;
  2142. end;
  2143. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2144. var
  2145. asmkind: tasm;
  2146. begin
  2147. for asmkind in [as_gas,as_ggas,as_darwin] do
  2148. if assigned(asminfos[asmkind]) and
  2149. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2150. begin
  2151. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2152. exit;
  2153. end;
  2154. Internalerror(2015090604);
  2155. end;
  2156. {*****************************************************************************
  2157. Init/Done
  2158. *****************************************************************************}
  2159. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2160. var
  2161. t : tasm;
  2162. begin
  2163. t:=r.id;
  2164. if assigned(asminfos[t]) then
  2165. writeln('Warning: Assembler is already registered!')
  2166. else
  2167. Getmem(asminfos[t],sizeof(tasminfo));
  2168. asminfos[t]^:=r;
  2169. CAssembler[t]:=c;
  2170. end;
  2171. end.