assemble.pas 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  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,triplet,
  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_gas,as_clang_llvm,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. if af_llvm in target_asm.flags then
  793. Replace(result,'$TRIPLET',targettriplet(triplet_llvm))
  794. {$ifdef arm}
  795. else if (target_info.system=system_arm_ios) then
  796. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]))
  797. {$endif arm}
  798. ;
  799. if (cs_link_on_target in current_settings.globalswitches) then
  800. begin
  801. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  802. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  803. end
  804. else
  805. begin
  806. {$ifdef hasunix}
  807. if DoPipe then
  808. if not(asminfo^.id in [as_clang_gas,as_clang_asdarwin,as_clang_llvm]) then
  809. Replace(result,'$ASM','')
  810. else
  811. Replace(result,'$ASM','-')
  812. else
  813. {$endif}
  814. Replace(result,'$ASM',maybequoted(AsmFileName));
  815. Replace(result,'$OBJ',maybequoted(ObjFileName));
  816. end;
  817. if (cs_create_pic in current_settings.moduleswitches) then
  818. Replace(result,'$PIC','-KPIC')
  819. else
  820. Replace(result,'$PIC','');
  821. if (cs_asm_source in current_settings.globalswitches) then
  822. Replace(result,'$NOWARN','')
  823. else
  824. Replace(result,'$NOWARN','-W');
  825. if target_info.endian=endian_little then
  826. Replace(result,'$ENDIAN','-mlittle')
  827. else
  828. Replace(result,'$ENDIAN','-mbig');
  829. { as we don't keep track of the amount of sections we created we simply
  830. enable Big Obj COFF files always for targets that need them }
  831. if (cs_asm_pre_binutils_2_25 in current_settings.globalswitches) or
  832. not (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) or
  833. (section_high_bound<min_big_obj_section_count) then
  834. Replace(result,'$BIGOBJ','')
  835. else
  836. Replace(result,'$BIGOBJ','-mbig-obj');
  837. Replace(result,'$EXTRAOPT',asmextraopt);
  838. end;
  839. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  840. var
  841. module : tmodule;
  842. begin
  843. { load infile }
  844. if (lastfileinfo.moduleindex<>hp.fileinfo.moduleindex) or
  845. (lastfileinfo.fileindex<>hp.fileinfo.fileindex) then
  846. begin
  847. { in case of a generic the module can be different }
  848. if current_module.unit_index=hp.fileinfo.moduleindex then
  849. module:=current_module
  850. else
  851. module:=get_module(hp.fileinfo.moduleindex);
  852. { during the compilation of the system unit there are cases when
  853. the fileinfo contains just zeros => invalid }
  854. if assigned(module) then
  855. infile:=module.sourcefiles.get_file(hp.fileinfo.fileindex)
  856. else
  857. infile:=nil;
  858. if assigned(infile) then
  859. begin
  860. { open only if needed !! }
  861. if (cs_asm_source in current_settings.globalswitches) then
  862. infile.open;
  863. end;
  864. { avoid unnecessary reopens of the same file !! }
  865. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  866. lastfileinfo.moduleindex:=hp.fileinfo.moduleindex;
  867. { be sure to change line !! }
  868. lastfileinfo.line:=-1;
  869. end;
  870. { write source }
  871. if (cs_asm_source in current_settings.globalswitches) and
  872. assigned(infile) then
  873. begin
  874. if (infile<>lastinfile) then
  875. begin
  876. writer.AsmWriteLn(asminfo^.comment+'['+infile.name+']');
  877. if assigned(lastinfile) then
  878. lastinfile.close;
  879. end;
  880. if (hp.fileinfo.line<>lastfileinfo.line) and
  881. (hp.fileinfo.line<infile.maxlinebuf) then
  882. begin
  883. if (hp.fileinfo.line<>0) and
  884. (infile.linebuf^[hp.fileinfo.line]>=0) then
  885. writer.AsmWriteLn(asminfo^.comment+'['+tostr(hp.fileinfo.line)+'] '+
  886. fixline(infile.GetLineStr(hp.fileinfo.line)));
  887. { set it to a negative value !
  888. to make that is has been read already !! PM }
  889. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  890. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  891. end;
  892. end;
  893. lastfileinfo:=hp.fileinfo;
  894. lastinfile:=infile;
  895. end;
  896. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  897. begin
  898. {$ifdef EXTDEBUG}
  899. if assigned(hp.problem) then
  900. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  901. tostr(hp.tempsize)+' '+hp.problem^)
  902. else
  903. {$endif EXTDEBUG}
  904. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  905. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  906. end;
  907. procedure TExternalAssembler.WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  908. var
  909. pdata: pbyte;
  910. index, step, swapmask, count: longint;
  911. ssingle: single;
  912. ddouble: double;
  913. ccomp: comp;
  914. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  915. eextended: extended;
  916. {$else}
  917. {$ifdef FPC_SOFT_FPUX80}
  918. eextended: floatx80;
  919. {$endif}
  920. {$endif cpuextended}
  921. begin
  922. if do_line then
  923. begin
  924. case tai_realconst(hp).realtyp of
  925. aitrealconst_s32bit:
  926. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  927. aitrealconst_s64bit:
  928. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  929. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  930. { can't write full 80 bit floating point constants yet on non-x86 }
  931. aitrealconst_s80bit:
  932. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  933. {$else}
  934. {$ifdef FPC_SOFT_FPUX80}
  935. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  936. aitrealconst_s80bit:
  937. begin
  938. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  939. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  940. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  941. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  942. else
  943. internalerror(2017091901);
  944. end;
  945. {$pop}
  946. {$endif}
  947. {$endif cpuextended}
  948. aitrealconst_s64comp:
  949. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  950. else
  951. internalerror(2014050604);
  952. end;
  953. end;
  954. writer.AsmWrite(dbdir);
  955. { generic float writing code: get start address of value, then write
  956. byte by byte. Can't use fields directly, because e.g ts64comp is
  957. defined as extended on x86 }
  958. case tai_realconst(hp).realtyp of
  959. aitrealconst_s32bit:
  960. begin
  961. ssingle:=single(tai_realconst(hp).value.s32val);
  962. pdata:=@ssingle;
  963. end;
  964. aitrealconst_s64bit:
  965. begin
  966. ddouble:=double(tai_realconst(hp).value.s64val);
  967. pdata:=@ddouble;
  968. end;
  969. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  970. { can't write full 80 bit floating point constants yet on non-x86 }
  971. aitrealconst_s80bit:
  972. begin
  973. eextended:=extended(tai_realconst(hp).value.s80val);
  974. pdata:=@eextended;
  975. end;
  976. {$else}
  977. {$ifdef FPC_SOFT_FPUX80}
  978. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  979. aitrealconst_s80bit:
  980. begin
  981. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  982. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  983. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  984. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  985. else
  986. internalerror(2017091901);
  987. pdata:=@eextended;
  988. end;
  989. {$pop}
  990. {$endif}
  991. {$endif cpuextended}
  992. aitrealconst_s64comp:
  993. begin
  994. ccomp:=comp(tai_realconst(hp).value.s64compval);
  995. pdata:=@ccomp;
  996. end;
  997. else
  998. internalerror(2014051001);
  999. end;
  1000. count:=tai_realconst(hp).datasize;
  1001. { write bytes in inverse order if source and target endianess don't
  1002. match }
  1003. if source_info.endian<>target_info.endian then
  1004. begin
  1005. { go from back to front }
  1006. index:=count-1;
  1007. step:=-1;
  1008. end
  1009. else
  1010. begin
  1011. index:=0;
  1012. step:=1;
  1013. end;
  1014. {$ifdef ARM}
  1015. { ARM-specific: low and high dwords of a double may be swapped }
  1016. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  1017. begin
  1018. { only supported for double }
  1019. if tai_realconst(hp).datasize<>8 then
  1020. internalerror(2014050605);
  1021. { switch bit of the index so that the words are written in
  1022. the opposite order }
  1023. swapmask:=4;
  1024. end
  1025. else
  1026. {$endif ARM}
  1027. swapmask:=0;
  1028. repeat
  1029. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  1030. inc(index,step);
  1031. dec(count);
  1032. if count<>0 then
  1033. writer.AsmWrite(',');
  1034. until count=0;
  1035. { padding }
  1036. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  1037. writer.AsmWrite(',0');
  1038. writer.AsmLn;
  1039. end;
  1040. procedure TExternalAssembler.WriteTree(p:TAsmList);
  1041. begin
  1042. end;
  1043. procedure TExternalAssembler.WriteAsmList;
  1044. begin
  1045. end;
  1046. procedure TExternalAssembler.MakeObject;
  1047. begin
  1048. writer.AsmCreate(cut_normal);
  1049. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  1050. lastfileinfo.line := -1;
  1051. lastinfile := nil;
  1052. lastsectype := sec_none;
  1053. WriteAsmList;
  1054. writer.AsmClose;
  1055. if not(writer.ioerror) then
  1056. DoAssemble;
  1057. end;
  1058. destructor TExternalAssembler.Destroy;
  1059. begin
  1060. if ffreewriter then
  1061. writer.Free;
  1062. inherited;
  1063. end;
  1064. {*****************************************************************************
  1065. TInternalAssembler
  1066. *****************************************************************************}
  1067. constructor TInternalAssembler.Create(info: pasminfo; smart: boolean);
  1068. begin
  1069. inherited;
  1070. ObjOutput:=nil;
  1071. ObjData:=nil;
  1072. SmartAsm:=smart;
  1073. end;
  1074. destructor TInternalAssembler.destroy;
  1075. begin
  1076. if assigned(ObjData) then
  1077. ObjData.free;
  1078. if assigned(ObjOutput) then
  1079. ObjOutput.free;
  1080. end;
  1081. procedure TInternalAssembler.WriteStab(p:pchar);
  1082. function consumecomma(var p:pchar):boolean;
  1083. begin
  1084. while (p^=' ') do
  1085. inc(p);
  1086. result:=(p^=',');
  1087. inc(p);
  1088. end;
  1089. function consumenumber(var p:pchar;out value:longint):boolean;
  1090. var
  1091. hs : string;
  1092. len,
  1093. code : integer;
  1094. begin
  1095. value:=0;
  1096. while (p^=' ') do
  1097. inc(p);
  1098. len:=0;
  1099. while (p^ in ['0'..'9']) do
  1100. begin
  1101. inc(len);
  1102. hs[len]:=p^;
  1103. inc(p);
  1104. end;
  1105. if len>0 then
  1106. begin
  1107. hs[0]:=chr(len);
  1108. val(hs,value,code);
  1109. end
  1110. else
  1111. code:=-1;
  1112. result:=(code=0);
  1113. end;
  1114. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  1115. var
  1116. hs : string;
  1117. len,
  1118. code : integer;
  1119. pstart : pchar;
  1120. sym : tobjsymbol;
  1121. exprvalue : longint;
  1122. gotmin,
  1123. have_first_symbol,
  1124. have_second_symbol,
  1125. dosub : boolean;
  1126. begin
  1127. result:=false;
  1128. value:=0;
  1129. relocsym:=nil;
  1130. gotmin:=false;
  1131. have_first_symbol:=false;
  1132. have_second_symbol:=false;
  1133. repeat
  1134. dosub:=false;
  1135. exprvalue:=0;
  1136. if gotmin then
  1137. begin
  1138. dosub:=true;
  1139. gotmin:=false;
  1140. end;
  1141. while (p^=' ') do
  1142. inc(p);
  1143. case p^ of
  1144. #0 :
  1145. break;
  1146. ' ' :
  1147. inc(p);
  1148. '0'..'9' :
  1149. begin
  1150. len:=0;
  1151. while (p^ in ['0'..'9']) do
  1152. begin
  1153. inc(len);
  1154. hs[len]:=p^;
  1155. inc(p);
  1156. end;
  1157. hs[0]:=chr(len);
  1158. val(hs,exprvalue,code);
  1159. if code<>0 then
  1160. internalerror(200702251);
  1161. end;
  1162. '.','_',
  1163. 'A'..'Z',
  1164. 'a'..'z' :
  1165. begin
  1166. pstart:=p;
  1167. while not(p^ in [#0,' ','-','+']) do
  1168. inc(p);
  1169. len:=p-pstart;
  1170. if len>255 then
  1171. internalerror(200509187);
  1172. move(pstart^,hs[1],len);
  1173. hs[0]:=chr(len);
  1174. sym:=objdata.symbolref(hs);
  1175. { Second symbol? }
  1176. if assigned(relocsym) then
  1177. begin
  1178. if have_second_symbol then
  1179. internalerror(2007032201);
  1180. have_second_symbol:=true;
  1181. if not have_first_symbol then
  1182. internalerror(2007032202);
  1183. { second symbol should substracted to first }
  1184. if not dosub then
  1185. internalerror(2007032203);
  1186. if (relocsym.objsection<>sym.objsection) then
  1187. internalerror(2005091810);
  1188. exprvalue:=relocsym.address-sym.address;
  1189. relocsym:=nil;
  1190. dosub:=false;
  1191. end
  1192. else
  1193. begin
  1194. relocsym:=sym;
  1195. if assigned(sym.objsection) then
  1196. begin
  1197. { first symbol should be + }
  1198. if not have_first_symbol and dosub then
  1199. internalerror(2007032204);
  1200. have_first_symbol:=true;
  1201. end;
  1202. end;
  1203. end;
  1204. '+' :
  1205. begin
  1206. { nothing, by default addition is done }
  1207. inc(p);
  1208. end;
  1209. '-' :
  1210. begin
  1211. gotmin:=true;
  1212. inc(p);
  1213. end;
  1214. else
  1215. internalerror(200509189);
  1216. end;
  1217. if dosub then
  1218. dec(value,exprvalue)
  1219. else
  1220. inc(value,exprvalue);
  1221. until false;
  1222. result:=true;
  1223. end;
  1224. var
  1225. stabstrlen,
  1226. ofs,
  1227. nline,
  1228. nidx,
  1229. nother,
  1230. i : longint;
  1231. stab : TObjStabEntry;
  1232. relocsym : TObjSymbol;
  1233. pstr,
  1234. pcurr,
  1235. pendquote : pchar;
  1236. oldsec : TObjSection;
  1237. begin
  1238. pcurr:=nil;
  1239. pstr:=nil;
  1240. pendquote:=nil;
  1241. relocsym:=nil;
  1242. ofs:=0;
  1243. { Parse string part }
  1244. if (p[0]='"') then
  1245. begin
  1246. pstr:=@p[1];
  1247. { Ignore \" inside the string }
  1248. i:=1;
  1249. while not((p[i]='"') and (p[i-1]<>'\')) and
  1250. (p[i]<>#0) do
  1251. inc(i);
  1252. pendquote:=@p[i];
  1253. pendquote^:=#0;
  1254. pcurr:=@p[i+1];
  1255. if not consumecomma(pcurr) then
  1256. internalerror(200509181);
  1257. end
  1258. else
  1259. pcurr:=p;
  1260. { When in pass 1 then only alloc and leave }
  1261. if ObjData.currpass=1 then
  1262. begin
  1263. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  1264. if assigned(pstr) and (pstr[0]<>#0) then
  1265. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  1266. end
  1267. else
  1268. begin
  1269. { Stabs format: nidx,nother,nline[,offset] }
  1270. if not consumenumber(pcurr,nidx) then
  1271. internalerror(200509182);
  1272. if not consumecomma(pcurr) then
  1273. internalerror(200509183);
  1274. if not consumenumber(pcurr,nother) then
  1275. internalerror(200509184);
  1276. if not consumecomma(pcurr) then
  1277. internalerror(200509185);
  1278. if not consumenumber(pcurr,nline) then
  1279. internalerror(200509186);
  1280. if consumecomma(pcurr) then
  1281. consumeoffset(pcurr,relocsym,ofs);
  1282. { Generate stab entry }
  1283. if assigned(pstr) and (pstr[0]<>#0) then
  1284. begin
  1285. stabstrlen:=strlen(pstr);
  1286. {$ifdef optimizestabs}
  1287. StabStrEntry:=nil;
  1288. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  1289. begin
  1290. hs:=strpas(pstr);
  1291. StabstrEntry:=StabStrDict.Find(hs);
  1292. if not assigned(StabstrEntry) then
  1293. begin
  1294. StabstrEntry:=TStabStrEntry.Create(hs);
  1295. StabstrEntry:=StabStrSec.Size;
  1296. StabStrDict.Insert(StabstrEntry);
  1297. { generate new stab }
  1298. StabstrEntry:=nil;
  1299. end;
  1300. end;
  1301. if assigned(StabstrEntry) then
  1302. stab.strpos:=StabstrEntry.strpos
  1303. else
  1304. {$endif optimizestabs}
  1305. begin
  1306. stab.strpos:=ObjData.StabStrSec.Size;
  1307. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  1308. end;
  1309. end
  1310. else
  1311. stab.strpos:=0;
  1312. stab.ntype:=byte(nidx);
  1313. stab.ndesc:=word(nline);
  1314. stab.nother:=byte(nother);
  1315. stab.nvalue:=ofs;
  1316. { Write the stab first without the value field. Then
  1317. write a the value field with relocation }
  1318. oldsec:=ObjData.CurrObjSec;
  1319. ObjData.SetSection(ObjData.StabsSec);
  1320. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  1321. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  1322. ObjData.setsection(oldsec);
  1323. end;
  1324. if assigned(pendquote) then
  1325. pendquote^:='"';
  1326. end;
  1327. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  1328. begin
  1329. { maybe end of list }
  1330. while not assigned(hp) do
  1331. begin
  1332. if currlistidx<lists then
  1333. begin
  1334. inc(currlistidx);
  1335. currlist:=list[currlistidx];
  1336. hp:=Tai(currList.first);
  1337. end
  1338. else
  1339. begin
  1340. MaybeNextList:=false;
  1341. exit;
  1342. end;
  1343. end;
  1344. MaybeNextList:=true;
  1345. end;
  1346. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  1347. var
  1348. objsym : TObjSymbol;
  1349. indsym : TObjSymbol;
  1350. begin
  1351. Result:=
  1352. Assigned(hp) and
  1353. (hp.typ=ait_symbol);
  1354. if not Result then
  1355. Exit;
  1356. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1357. objsym.size:=0;
  1358. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1359. if not Assigned(indsym) then
  1360. begin
  1361. { it's possible that indirect symbol is not present in the list,
  1362. so we must create it as undefined }
  1363. indsym:=ObjData.CObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1364. indsym.typ:=AT_NONE;
  1365. indsym.bind:=AB_NONE;
  1366. end;
  1367. objsym.indsymbol:=indsym;
  1368. Result:=true;
  1369. end;
  1370. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1371. var
  1372. objsym,
  1373. objsymend : TObjSymbol;
  1374. cpu: tcputype;
  1375. begin
  1376. while assigned(hp) do
  1377. begin
  1378. case hp.typ of
  1379. ait_align :
  1380. begin
  1381. if tai_align_abstract(hp).aligntype>1 then
  1382. begin
  1383. { always use the maximum fillsize in this pass to avoid possible
  1384. short jumps to become out of range }
  1385. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1386. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1387. { may need to increase alignment of section }
  1388. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1389. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1390. end
  1391. else
  1392. Tai_align_abstract(hp).fillsize:=0;
  1393. end;
  1394. ait_datablock :
  1395. begin
  1396. {$ifdef USE_COMM_IN_BSS}
  1397. if writingpackages and
  1398. Tai_datablock(hp).is_global then
  1399. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1400. else
  1401. {$endif USE_COMM_IN_BSS}
  1402. begin
  1403. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1404. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1405. ObjData.alloc(Tai_datablock(hp).size);
  1406. end;
  1407. end;
  1408. ait_realconst:
  1409. ObjData.alloc(tai_realconst(hp).savesize);
  1410. ait_const:
  1411. begin
  1412. { if symbols are provided we can calculate the value for relative symbols.
  1413. This is required for length calculation of leb128 constants }
  1414. if assigned(tai_const(hp).sym) then
  1415. begin
  1416. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1417. { objsym already defined and there is endsym? }
  1418. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1419. begin
  1420. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1421. { objsymend already defined? }
  1422. if assigned(objsymend.objsection) then
  1423. begin
  1424. if objsymend.objsection<>objsym.objsection then
  1425. begin
  1426. { leb128 relative constants are not relocatable, but other types are,
  1427. given that objsym belongs to the current section. }
  1428. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1429. (objsym.objsection<>ObjData.CurrObjSec) then
  1430. InternalError(200404124);
  1431. end
  1432. else
  1433. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1434. end;
  1435. end;
  1436. end;
  1437. ObjData.alloc(tai_const(hp).size);
  1438. end;
  1439. ait_directive:
  1440. begin
  1441. case tai_directive(hp).directive of
  1442. asd_indirect_symbol:
  1443. { handled in TreePass1 }
  1444. ;
  1445. asd_lazy_reference:
  1446. begin
  1447. if tai_directive(hp).name='' then
  1448. Internalerror(2009112101);
  1449. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1450. objsym.bind:=AB_LAZY;
  1451. end;
  1452. asd_reference:
  1453. { ignore for now, but should be added}
  1454. ;
  1455. asd_cpu:
  1456. begin
  1457. ObjData.CPUType:=cpu_none;
  1458. for cpu:=low(tcputype) to high(tcputype) do
  1459. if cputypestr[cpu]=tai_directive(hp).name then
  1460. begin
  1461. ObjData.CPUType:=cpu;
  1462. break;
  1463. end;
  1464. end;
  1465. asd_weak_definition:
  1466. begin
  1467. if tai_directive(hp).name='' then
  1468. Internalerror(2022040901);
  1469. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1470. objsym.bind:=AB_WEAK;
  1471. end;
  1472. {$ifdef OMFOBJSUPPORT}
  1473. asd_omf_linnum_line:
  1474. { ignore for now, but should be added}
  1475. ;
  1476. {$endif OMFOBJSUPPORT}
  1477. {$ifdef ARM}
  1478. asd_thumb_func:
  1479. ObjData.ThumbFunc:=true;
  1480. asd_code:
  1481. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1482. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1483. {$endif ARM}
  1484. else
  1485. internalerror(2010011101);
  1486. end;
  1487. end;
  1488. ait_section:
  1489. begin
  1490. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1491. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1492. end;
  1493. ait_symbol :
  1494. begin
  1495. { needs extra support in the internal assembler }
  1496. { the value is just ignored }
  1497. {if tai_symbol(hp).has_value then
  1498. internalerror(2009090804); ;}
  1499. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1500. end;
  1501. ait_label :
  1502. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1503. ait_string :
  1504. ObjData.alloc(Tai_string(hp).len);
  1505. ait_instruction :
  1506. begin
  1507. { reset instructions which could change in pass 2 }
  1508. Taicpu(hp).resetpass2;
  1509. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1510. end;
  1511. ait_cutobject :
  1512. if SmartAsm then
  1513. break;
  1514. end;
  1515. hp:=Tai(hp.next);
  1516. end;
  1517. TreePass0:=hp;
  1518. end;
  1519. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1520. var
  1521. objsym,
  1522. objsymend : TObjSymbol;
  1523. cpu: tcputype;
  1524. begin
  1525. while assigned(hp) do
  1526. begin
  1527. case hp.typ of
  1528. ait_align :
  1529. begin
  1530. if tai_align_abstract(hp).aligntype>1 then
  1531. begin
  1532. { here we must determine the fillsize which is used in pass2 }
  1533. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1534. ObjData.CurrObjSec.Size;
  1535. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1536. end;
  1537. end;
  1538. ait_datablock :
  1539. begin
  1540. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1541. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1542. Message(asmw_e_alloc_data_only_in_bss);
  1543. {$ifdef USE_COMM_IN_BSS}
  1544. if writingpackages and
  1545. Tai_datablock(hp).is_global then
  1546. begin
  1547. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1548. objsym.size:=Tai_datablock(hp).size;
  1549. objsym.bind:=AB_COMMON;
  1550. objsym.alignment:=needtowritealignmentalsoforELF;
  1551. end
  1552. else
  1553. {$endif USE_COMM_IN_BSS}
  1554. begin
  1555. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1556. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1557. objsym.size:=Tai_datablock(hp).size;
  1558. ObjData.alloc(Tai_datablock(hp).size);
  1559. end;
  1560. end;
  1561. ait_realconst:
  1562. ObjData.alloc(tai_realconst(hp).savesize);
  1563. ait_const:
  1564. begin
  1565. { Recalculate relative symbols }
  1566. if assigned(tai_const(hp).sym) and
  1567. assigned(tai_const(hp).endsym) then
  1568. begin
  1569. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1570. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1571. if objsymend.objsection<>objsym.objsection then
  1572. begin
  1573. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1574. (objsym.objsection<>ObjData.CurrObjSec) then
  1575. internalerror(200905042);
  1576. end
  1577. else
  1578. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1579. end;
  1580. ObjData.alloc(tai_const(hp).size);
  1581. end;
  1582. ait_section:
  1583. begin
  1584. { use cached value }
  1585. ObjData.setsection(Tai_section(hp).sec);
  1586. end;
  1587. ait_stab :
  1588. begin
  1589. if assigned(Tai_stab(hp).str) then
  1590. WriteStab(Tai_stab(hp).str);
  1591. end;
  1592. ait_symbol :
  1593. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1594. ait_symbol_end :
  1595. begin
  1596. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1597. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1598. end;
  1599. ait_label :
  1600. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1601. ait_string :
  1602. ObjData.alloc(Tai_string(hp).len);
  1603. ait_instruction :
  1604. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1605. ait_cutobject :
  1606. if SmartAsm then
  1607. break;
  1608. ait_directive :
  1609. begin
  1610. case tai_directive(hp).directive of
  1611. asd_indirect_symbol:
  1612. if tai_directive(hp).name='' then
  1613. Internalerror(2009101103)
  1614. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1615. Internalerror(2009101102);
  1616. asd_lazy_reference:
  1617. { handled in TreePass0 }
  1618. ;
  1619. asd_reference:
  1620. { ignore for now, but should be added}
  1621. ;
  1622. asd_thumb_func:
  1623. { ignore for now, but should be added}
  1624. ;
  1625. asd_code:
  1626. { ignore for now, but should be added}
  1627. ;
  1628. asd_weak_definition:
  1629. { ignore for now, but should be added}
  1630. ;
  1631. {$ifdef OMFOBJSUPPORT}
  1632. asd_omf_linnum_line:
  1633. { ignore for now, but should be added}
  1634. ;
  1635. {$endif OMFOBJSUPPORT}
  1636. asd_cpu:
  1637. begin
  1638. ObjData.CPUType:=cpu_none;
  1639. for cpu:=low(tcputype) to high(tcputype) do
  1640. if cputypestr[cpu]=tai_directive(hp).name then
  1641. begin
  1642. ObjData.CPUType:=cpu;
  1643. break;
  1644. end;
  1645. end;
  1646. else
  1647. internalerror(2010011102);
  1648. end;
  1649. end;
  1650. end;
  1651. hp:=Tai(hp.next);
  1652. end;
  1653. TreePass1:=hp;
  1654. end;
  1655. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1656. var
  1657. fillbuffer : tfillbuffer;
  1658. leblen : byte;
  1659. lebbuf : array[0..63] of byte;
  1660. objsym,
  1661. ref,
  1662. objsymend : TObjSymbol;
  1663. zerobuf : array[0..63] of byte;
  1664. relative_reloc: boolean;
  1665. pdata : pointer;
  1666. ssingle : single;
  1667. ddouble : double;
  1668. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1669. eextended : extended;
  1670. {$else}
  1671. {$ifdef FPC_SOFT_FPUX80}
  1672. eextended : floatx80;
  1673. {$endif}
  1674. {$endif}
  1675. ccomp : comp;
  1676. tmp : word;
  1677. cpu: tcputype;
  1678. begin
  1679. fillchar(zerobuf,sizeof(zerobuf),0);
  1680. fillchar(objsym,sizeof(objsym),0);
  1681. fillchar(objsymend,sizeof(objsymend),0);
  1682. { main loop }
  1683. while assigned(hp) do
  1684. begin
  1685. case hp.typ of
  1686. ait_align :
  1687. begin
  1688. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1689. InternalError(2012072301);
  1690. if oso_data in ObjData.CurrObjSec.secoptions then
  1691. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1692. Tai_align_abstract(hp).fillsize)
  1693. else
  1694. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1695. end;
  1696. ait_section :
  1697. begin
  1698. { use cached value }
  1699. ObjData.setsection(Tai_section(hp).sec);
  1700. end;
  1701. ait_symbol :
  1702. begin
  1703. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1704. end;
  1705. ait_symbol_end :
  1706. begin
  1707. { recalculate size, as some preceding instructions
  1708. could have been changed to smaller size }
  1709. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1710. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1711. end;
  1712. ait_datablock :
  1713. begin
  1714. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1715. {$ifdef USE_COMM_IN_BSS}
  1716. if not(writingpackages and
  1717. Tai_datablock(hp).is_global) then
  1718. {$endif USE_COMM_IN_BSS}
  1719. begin
  1720. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1721. ObjData.alloc(Tai_datablock(hp).size);
  1722. end;
  1723. end;
  1724. ait_realconst:
  1725. begin
  1726. case tai_realconst(hp).realtyp of
  1727. aitrealconst_s32bit:
  1728. begin
  1729. ssingle:=single(tai_realconst(hp).value.s32val);
  1730. pdata:=@ssingle;
  1731. end;
  1732. aitrealconst_s64bit:
  1733. begin
  1734. ddouble:=double(tai_realconst(hp).value.s64val);
  1735. pdata:=@ddouble;
  1736. end;
  1737. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1738. { can't write full 80 bit floating point constants yet on non-x86 }
  1739. aitrealconst_s80bit:
  1740. begin
  1741. eextended:=extended(tai_realconst(hp).value.s80val);
  1742. pdata:=@eextended;
  1743. end;
  1744. {$else}
  1745. {$ifdef FPC_SOFT_FPUX80}
  1746. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1747. aitrealconst_s80bit:
  1748. begin
  1749. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1750. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  1751. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1752. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  1753. else
  1754. internalerror(2017091901);
  1755. pdata:=@eextended;
  1756. end;
  1757. {$pop}
  1758. {$endif}
  1759. {$endif cpuextended}
  1760. aitrealconst_s64comp:
  1761. begin
  1762. ccomp:=comp(tai_realconst(hp).value.s64compval);
  1763. pdata:=@ccomp;
  1764. end;
  1765. else
  1766. internalerror(2015030501);
  1767. end;
  1768. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  1769. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  1770. end;
  1771. ait_string :
  1772. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1773. ait_const :
  1774. begin
  1775. { Recalculate relative symbols, addresses of forward references
  1776. can be changed in treepass1 }
  1777. relative_reloc:=false;
  1778. if assigned(tai_const(hp).sym) and
  1779. assigned(tai_const(hp).endsym) then
  1780. begin
  1781. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1782. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1783. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  1784. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1785. end;
  1786. case tai_const(hp).consttype of
  1787. aitconst_64bit,
  1788. aitconst_32bit,
  1789. aitconst_16bit,
  1790. aitconst_64bit_unaligned,
  1791. aitconst_32bit_unaligned,
  1792. aitconst_16bit_unaligned,
  1793. aitconst_8bit :
  1794. begin
  1795. if assigned(tai_const(hp).sym) and
  1796. not assigned(tai_const(hp).endsym) then
  1797. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  1798. else if relative_reloc then
  1799. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  1800. else
  1801. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1802. end;
  1803. aitconst_rva_symbol :
  1804. begin
  1805. { PE32+? }
  1806. if target_info.system=system_x86_64_win64 then
  1807. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  1808. else
  1809. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  1810. end;
  1811. aitconst_secrel32_symbol :
  1812. begin
  1813. { Required for DWARF2 support under Windows }
  1814. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  1815. end;
  1816. {$ifdef i8086}
  1817. aitconst_farptr :
  1818. if assigned(tai_const(hp).sym) and
  1819. not assigned(tai_const(hp).endsym) then
  1820. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  1821. else if relative_reloc then
  1822. internalerror(2015040601)
  1823. else
  1824. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1825. aitconst_seg:
  1826. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  1827. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  1828. else
  1829. internalerror(2015110502);
  1830. aitconst_dgroup:
  1831. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  1832. aitconst_fardataseg:
  1833. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  1834. {$endif i8086}
  1835. {$ifdef arm}
  1836. aitconst_got:
  1837. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  1838. {$endif arm}
  1839. aitconst_gotoff_symbol:
  1840. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  1841. aitconst_uleb128bit,
  1842. aitconst_sleb128bit :
  1843. begin
  1844. if tai_const(hp).consttype=aitconst_uleb128bit then
  1845. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf)
  1846. else
  1847. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf);
  1848. if leblen<>tai_const(hp).size then
  1849. internalerror(200709271);
  1850. ObjData.writebytes(lebbuf,leblen);
  1851. end;
  1852. aitconst_darwin_dwarf_delta32,
  1853. aitconst_darwin_dwarf_delta64:
  1854. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1855. aitconst_half16bit,
  1856. aitconst_gs:
  1857. begin
  1858. tmp:=Tai_const(hp).value div 2;
  1859. ObjData.writebytes(tmp,2);
  1860. end;
  1861. else
  1862. internalerror(200603254);
  1863. end;
  1864. end;
  1865. ait_label :
  1866. begin
  1867. { exporting shouldn't be necessary as labels are local,
  1868. but it's better to be on the safe side (PFV) }
  1869. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  1870. end;
  1871. ait_instruction :
  1872. Taicpu(hp).Pass2(ObjData);
  1873. ait_stab :
  1874. WriteStab(Tai_stab(hp).str);
  1875. ait_function_name,
  1876. ait_force_line : ;
  1877. ait_cutobject :
  1878. if SmartAsm then
  1879. break;
  1880. ait_directive :
  1881. begin
  1882. case tai_directive(hp).directive of
  1883. asd_weak_definition,
  1884. asd_weak_reference:
  1885. begin
  1886. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1887. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  1888. objsym.bind:=AB_WEAK_EXTERNAL
  1889. else
  1890. { TODO: should become a weak definition; for now, do
  1891. the same as what was done for ait_weak }
  1892. objsym.bind:=AB_WEAK_EXTERNAL;
  1893. end;
  1894. asd_cpu:
  1895. begin
  1896. ObjData.CPUType:=cpu_none;
  1897. for cpu:=low(tcputype) to high(tcputype) do
  1898. if cputypestr[cpu]=tai_directive(hp).name then
  1899. begin
  1900. ObjData.CPUType:=cpu;
  1901. break;
  1902. end;
  1903. end;
  1904. {$ifdef OMFOBJSUPPORT}
  1905. asd_omf_linnum_line:
  1906. begin
  1907. TOmfObjSection(ObjData.CurrObjSec).LinNumEntries.Add(
  1908. TOmfSubRecord_LINNUM_MsLink_Entry.Create(
  1909. strtoint(tai_directive(hp).name),
  1910. ObjData.CurrObjSec.Size
  1911. ));
  1912. end;
  1913. {$endif OMFOBJSUPPORT}
  1914. end
  1915. end;
  1916. ait_symbolpair:
  1917. begin
  1918. if tai_symbolpair(hp).kind=spk_set then
  1919. begin
  1920. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  1921. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  1922. objsym.offset:=ref.offset;
  1923. objsym.objsection:=ref.objsection;
  1924. {$ifdef arm}
  1925. objsym.ThumbFunc:=ref.ThumbFunc;
  1926. {$endif arm}
  1927. end;
  1928. end;
  1929. {$ifndef DISABLE_WIN64_SEH}
  1930. ait_seh_directive :
  1931. tai_seh_directive(hp).generate_code(objdata);
  1932. {$endif DISABLE_WIN64_SEH}
  1933. end;
  1934. hp:=Tai(hp.next);
  1935. end;
  1936. TreePass2:=hp;
  1937. end;
  1938. procedure TInternalAssembler.writetree;
  1939. label
  1940. doexit;
  1941. var
  1942. hp : Tai;
  1943. ObjWriter : TObjectWriter;
  1944. begin
  1945. ObjWriter:=TObjectwriter.create;
  1946. ObjOutput:=CObjOutput.Create(ObjWriter);
  1947. ObjData:=ObjOutput.newObjData(ObjFileName);
  1948. { Pass 0 }
  1949. ObjData.currpass:=0;
  1950. ObjData.createsection(sec_code);
  1951. ObjData.beforealloc;
  1952. { start with list 1 }
  1953. currlistidx:=1;
  1954. currlist:=list[currlistidx];
  1955. hp:=Tai(currList.first);
  1956. while assigned(hp) do
  1957. begin
  1958. hp:=TreePass0(hp);
  1959. MaybeNextList(hp);
  1960. end;
  1961. ObjData.afteralloc;
  1962. { leave if errors have occurred }
  1963. if errorcount>0 then
  1964. goto doexit;
  1965. { Pass 1 }
  1966. ObjData.currpass:=1;
  1967. ObjData.resetsections;
  1968. ObjData.beforealloc;
  1969. ObjData.createsection(sec_code);
  1970. { start with list 1 }
  1971. currlistidx:=1;
  1972. currlist:=list[currlistidx];
  1973. hp:=Tai(currList.first);
  1974. while assigned(hp) do
  1975. begin
  1976. hp:=TreePass1(hp);
  1977. MaybeNextList(hp);
  1978. end;
  1979. ObjData.createsection(sec_code);
  1980. ObjData.afteralloc;
  1981. { leave if errors have occurred }
  1982. if errorcount>0 then
  1983. goto doexit;
  1984. { Pass 2 }
  1985. ObjData.currpass:=2;
  1986. ObjData.resetsections;
  1987. ObjData.beforewrite;
  1988. ObjData.createsection(sec_code);
  1989. { start with list 1 }
  1990. currlistidx:=1;
  1991. currlist:=list[currlistidx];
  1992. hp:=Tai(currList.first);
  1993. while assigned(hp) do
  1994. begin
  1995. hp:=TreePass2(hp);
  1996. MaybeNextList(hp);
  1997. end;
  1998. ObjData.createsection(sec_code);
  1999. ObjData.afterwrite;
  2000. { don't write the .o file if errors have occurred }
  2001. if errorcount=0 then
  2002. begin
  2003. { write objectfile }
  2004. ObjOutput.startobjectfile(ObjFileName);
  2005. ObjOutput.writeobjectfile(ObjData);
  2006. end;
  2007. doexit:
  2008. { Cleanup }
  2009. ObjData.free;
  2010. ObjData:=nil;
  2011. ObjWriter.free;
  2012. end;
  2013. procedure TInternalAssembler.writetreesmart;
  2014. var
  2015. hp : Tai;
  2016. startsectype : TAsmSectiontype;
  2017. place: tcutplace;
  2018. ObjWriter : TObjectWriter;
  2019. startsecname: String;
  2020. startsecorder: TAsmSectionOrder;
  2021. begin
  2022. if not(cs_asm_leave in current_settings.globalswitches) and
  2023. not(af_needar in asminfo^.flags) then
  2024. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  2025. else
  2026. ObjWriter:=TObjectwriter.create;
  2027. NextSmartName(cut_normal);
  2028. ObjOutput:=CObjOutput.Create(ObjWriter);
  2029. startsectype:=sec_none;
  2030. startsecname:='';
  2031. startsecorder:=secorder_default;
  2032. { start with list 1 }
  2033. currlistidx:=1;
  2034. currlist:=list[currlistidx];
  2035. hp:=Tai(currList.first);
  2036. while assigned(hp) do
  2037. begin
  2038. ObjData:=ObjOutput.newObjData(ObjFileName);
  2039. { Pass 0 }
  2040. ObjData.currpass:=0;
  2041. ObjData.resetsections;
  2042. ObjData.beforealloc;
  2043. if startsectype<>sec_none then
  2044. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2045. TreePass0(hp);
  2046. ObjData.afteralloc;
  2047. { leave if errors have occurred }
  2048. if errorcount>0 then
  2049. break;
  2050. { Pass 1 }
  2051. ObjData.currpass:=1;
  2052. ObjData.resetsections;
  2053. ObjData.beforealloc;
  2054. if startsectype<>sec_none then
  2055. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2056. TreePass1(hp);
  2057. ObjData.afteralloc;
  2058. { leave if errors have occurred }
  2059. if errorcount>0 then
  2060. break;
  2061. { Pass 2 }
  2062. ObjData.currpass:=2;
  2063. ObjOutput.startobjectfile(ObjFileName);
  2064. ObjData.resetsections;
  2065. ObjData.beforewrite;
  2066. if startsectype<>sec_none then
  2067. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2068. hp:=TreePass2(hp);
  2069. ObjData.afterwrite;
  2070. { leave if errors have occurred }
  2071. if errorcount>0 then
  2072. break;
  2073. { write the current objectfile }
  2074. ObjOutput.writeobjectfile(ObjData);
  2075. ObjData.free;
  2076. ObjData:=nil;
  2077. { end of lists? }
  2078. if not MaybeNextList(hp) then
  2079. break;
  2080. { we will start a new objectfile so reset everything }
  2081. { The place can still change in the next while loop, so don't init }
  2082. { the writer yet (JM) }
  2083. if (hp.typ=ait_cutobject) then
  2084. place := Tai_cutobject(hp).place
  2085. else
  2086. place := cut_normal;
  2087. { avoid empty files }
  2088. startsectype:=sec_none;
  2089. startsecname:='';
  2090. startsecorder:=secorder_default;
  2091. while assigned(hp) and
  2092. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2093. begin
  2094. if Tai(hp).typ=ait_section then
  2095. begin
  2096. startsectype:=Tai_section(hp).sectype;
  2097. startsecname:=Tai_section(hp).name^;
  2098. startsecorder:=Tai_section(hp).secorder;
  2099. end;
  2100. if (Tai(hp).typ=ait_cutobject) then
  2101. place:=Tai_cutobject(hp).place;
  2102. hp:=Tai(hp.next);
  2103. end;
  2104. if not MaybeNextList(hp) then
  2105. break;
  2106. { start next objectfile }
  2107. NextSmartName(place);
  2108. end;
  2109. ObjData.free;
  2110. ObjData:=nil;
  2111. ObjWriter.free;
  2112. end;
  2113. procedure TInternalAssembler.MakeObject;
  2114. var to_do:set of TasmlistType;
  2115. i:TasmlistType;
  2116. procedure addlist(p:TAsmList);
  2117. begin
  2118. inc(lists);
  2119. list[lists]:=p;
  2120. end;
  2121. begin
  2122. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2123. if usedeffileforexports then
  2124. exclude(to_do,al_exports);
  2125. if not(tf_section_threadvars in target_info.flags) then
  2126. exclude(to_do,al_threadvars);
  2127. for i:=low(TasmlistType) to high(TasmlistType) do
  2128. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2129. (not current_asmdata.asmlists[i].empty) then
  2130. addlist(current_asmdata.asmlists[i]);
  2131. if SmartAsm then
  2132. writetreesmart
  2133. else
  2134. writetree;
  2135. end;
  2136. {*****************************************************************************
  2137. Generate Assembler Files Main Procedure
  2138. *****************************************************************************}
  2139. Procedure GenerateAsm(smart:boolean);
  2140. var
  2141. a : TAssembler;
  2142. begin
  2143. if not assigned(CAssembler[target_asm.id]) then
  2144. Message(asmw_f_assembler_output_not_supported);
  2145. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2146. a.MakeObject;
  2147. a.Free;
  2148. end;
  2149. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2150. var
  2151. asmkind: tasm;
  2152. begin
  2153. for asmkind in [as_gas,as_ggas,as_darwin,as_clang_gas,as_clang_asdarwin] do
  2154. if assigned(asminfos[asmkind]) and
  2155. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2156. begin
  2157. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2158. exit;
  2159. end;
  2160. Internalerror(2015090604);
  2161. end;
  2162. {*****************************************************************************
  2163. Init/Done
  2164. *****************************************************************************}
  2165. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2166. var
  2167. t : tasm;
  2168. begin
  2169. t:=r.id;
  2170. if assigned(asminfos[t]) then
  2171. writeln('Warning: Assembler is already registered!')
  2172. else
  2173. Getmem(asminfos[t],sizeof(tasminfo));
  2174. asminfos[t]^:=r;
  2175. CAssembler[t]:=c;
  2176. end;
  2177. end.