assemble.pas 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832
  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 ResetSourceLines;
  128. procedure WriteSourceLine(hp: tailineinfo);
  129. procedure WriteTempalloc(hp: tai_tempalloc);
  130. procedure WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  131. function WriteComments(var hp: tai): boolean;
  132. function single2str(d : single) : string; virtual;
  133. function double2str(d : double) : string; virtual;
  134. function extended2str(e : extended) : string; virtual;
  135. function sleb128tostr(a : int64) : string;
  136. function uleb128tostr(a : qword) : string;
  137. Function DoPipe:boolean; virtual;
  138. function CreateNewAsmWriter: TExternalAssemblerOutputFile; virtual;
  139. {# Return true if the external assembler should run again }
  140. function RerunAssembler: boolean; virtual;
  141. public
  142. {# Returns the complete path and executable name of the assembler
  143. program.
  144. It first tries looking in the UTIL directory if specified,
  145. otherwise it searches in the free pascal binary directory, in
  146. the current working directory and then in the directories
  147. in the $PATH environment.}
  148. Function FindAssembler:string;
  149. {# Actually does the call to the assembler file. Returns false
  150. if the assembling of the file failed.}
  151. Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
  152. Function DoAssemble:boolean;virtual;
  153. {# This routine should be overridden for each assembler, it is used
  154. to actually write the abstract assembler stream to file.}
  155. procedure WriteTree(p:TAsmList);virtual;
  156. {# This routine should be overridden for each assembler, it is used
  157. to actually write all the different abstract assembler streams
  158. by calling for each stream type, the @var(WriteTree) method.}
  159. procedure WriteAsmList;virtual;
  160. {# Constructs the command line for calling the assembler }
  161. function MakeCmdLine: TCmdStr; virtual;
  162. public
  163. Constructor Create(info: pasminfo; smart: boolean); override; final;
  164. Constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); virtual;
  165. procedure MakeObject;override;
  166. destructor Destroy; override;
  167. property writer: TExternalAssemblerOutputFile read fwriter;
  168. end;
  169. TExternalAssemblerClass = class of TExternalAssembler;
  170. { TInternalAssembler }
  171. TInternalAssembler=class(TAssembler)
  172. private
  173. {$ifdef ARM}
  174. { true, if thumb instructions are generated }
  175. Code16 : Boolean;
  176. {$endif ARM}
  177. FCObjOutput : TObjOutputclass;
  178. FCInternalAr : TObjectWriterClass;
  179. { the aasmoutput lists that need to be processed }
  180. lists : byte;
  181. list : array[1..maxoutputlists] of TAsmList;
  182. { current processing }
  183. currlistidx : byte;
  184. currlist : TAsmList;
  185. procedure WriteStab(p:pchar);
  186. function MaybeNextList(var hp:Tai):boolean;
  187. function SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  188. function TreePass0(hp:Tai):Tai;
  189. function TreePass1(hp:Tai):Tai;
  190. function TreePass2(hp:Tai):Tai;
  191. procedure writetree;
  192. procedure writetreesmart;
  193. protected
  194. ObjData : TObjData;
  195. ObjOutput : tObjOutput;
  196. property CObjOutput:TObjOutputclass read FCObjOutput write FCObjOutput;
  197. property CInternalAr : TObjectWriterClass read FCInternalAr write FCInternalAr;
  198. public
  199. constructor Create(info: pasminfo; smart: boolean);override;
  200. destructor destroy;override;
  201. procedure MakeObject;override;
  202. end;
  203. TAssemblerClass = class of TAssembler;
  204. Procedure GenerateAsm(smart:boolean);
  205. { get an instance of an external GNU-style assembler that is compatible
  206. with the current target, reusing an existing writer. Used by the LLVM
  207. target to write inline assembler }
  208. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  209. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  210. Implementation
  211. uses
  212. {$ifdef hasunix}
  213. unix,
  214. {$endif}
  215. cutils,cfileutl,
  216. {$ifdef memdebug}
  217. cclasses,
  218. {$endif memdebug}
  219. {$ifdef OMFOBJSUPPORT}
  220. omfbase,
  221. ogomf,
  222. {$endif OMFOBJSUPPORT}
  223. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  224. {$else}
  225. {$ifdef FPC_SOFT_FPUX80}
  226. sfpux80,
  227. {$endif FPC_SOFT_FPUX80}
  228. {$endif}
  229. {$ifdef WASM}
  230. ogwasm,
  231. {$endif WASM}
  232. cscript,fmodule,verbose,
  233. cpubase,cpuinfo,triplet,
  234. aasmcpu;
  235. var
  236. CAssembler : array[tasm] of TAssemblerClass;
  237. function fixline(const s:string):string;
  238. {
  239. return s with all leading and ending spaces and tabs removed
  240. }
  241. var
  242. i,j,k : integer;
  243. begin
  244. i:=length(s);
  245. while (i>0) and (s[i] in [#9,' ']) do
  246. dec(i);
  247. j:=1;
  248. while (j<i) and (s[j] in [#9,' ']) do
  249. inc(j);
  250. result := Copy(s, j, i - j + 1);
  251. for k:=1 to length(result) do
  252. if result[k] in [#0..#31,#127..#255] then
  253. result[k]:='.';
  254. end;
  255. {*****************************************************************************
  256. TAssembler
  257. *****************************************************************************}
  258. Constructor TAssembler.Create(info: pasminfo; smart: boolean);
  259. begin
  260. asminfo:=info;
  261. { load start values }
  262. AsmFileName:=current_module.AsmFilename;
  263. ObjFileName:=current_module.ObjFileName;
  264. name:=Lower(current_module.modulename^);
  265. path:=current_module.outputpath;
  266. asmprefix := current_module.asmprefix^;
  267. if current_module.outputpath = '' then
  268. ppufilename := ''
  269. else
  270. ppufilename := current_module.ppufilename;
  271. SmartAsm:=smart;
  272. SmartFilesCount:=0;
  273. SmartHeaderCount:=0;
  274. SmartLinkOFiles.Clear;
  275. end;
  276. Destructor TAssembler.Destroy;
  277. begin
  278. end;
  279. procedure TAssembler.NextSmartName(place:tcutplace);
  280. var
  281. s : string;
  282. begin
  283. inc(SmartFilesCount);
  284. if SmartFilesCount>999999 then
  285. Message(asmw_f_too_many_asm_files);
  286. case place of
  287. cut_begin :
  288. begin
  289. inc(SmartHeaderCount);
  290. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  291. end;
  292. cut_normal :
  293. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  294. cut_end :
  295. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  296. end;
  297. AsmFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  298. ObjFileName:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  299. { insert in container so it can be cleared after the linking }
  300. SmartLinkOFiles.Insert(ObjFileName);
  301. end;
  302. {*****************************************************************************
  303. TAssemblerOutputFile
  304. *****************************************************************************}
  305. procedure TExternalAssemblerOutputFile.RemoveAsm;
  306. var
  307. g : file;
  308. begin
  309. if cs_asm_leave in current_settings.globalswitches then
  310. exit;
  311. if cs_asm_extern in current_settings.globalswitches then
  312. AsmRes.AddDeleteCommand(owner.AsmFileName)
  313. else
  314. begin
  315. assign(g,owner.AsmFileName);
  316. {$push} {$I-}
  317. erase(g);
  318. {$pop}
  319. if ioresult<>0 then;
  320. end;
  321. end;
  322. Procedure TExternalAssemblerOutputFile.AsmFlush;
  323. begin
  324. if outcnt>0 then
  325. begin
  326. { suppress i/o error }
  327. {$push} {$I-}
  328. BlockWrite(outfile,outbuf,outcnt);
  329. {$pop}
  330. fioerror:=fioerror or (ioresult<>0);
  331. outcnt:=0;
  332. end;
  333. end;
  334. procedure TExternalAssemblerOutputFile.MarkEmpty;
  335. begin
  336. AsmStartSize:=AsmSize
  337. end;
  338. function TExternalAssemblerOutputFile.ClearIfEmpty: boolean;
  339. begin
  340. result:=AsmSize=AsmStartSize;
  341. if result then
  342. AsmClear;
  343. end;
  344. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const c: char);
  345. begin
  346. MaybeAddLinePrefix;
  347. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(c));
  348. end;
  349. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const s: string);
  350. begin
  351. MaybeAddLinePrefix;
  352. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  353. end;
  354. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(const s: ansistring);
  355. begin
  356. MaybeAddLinePrefix;
  357. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  358. end;
  359. procedure TExternalAssemblerOutputFile.AsmWriteFiltered(p: pchar; len: longint);
  360. var
  361. s: ansistring;
  362. begin
  363. MaybeAddLinePrefix;
  364. s:='';
  365. setlength(s,len);
  366. move(p^,s[1],len);
  367. AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
  368. end;
  369. Procedure TExternalAssemblerOutputFile.AsmClear;
  370. begin
  371. outcnt:=0;
  372. end;
  373. procedure TExternalAssemblerOutputFile.MaybeAddLinePrefix;
  374. begin
  375. if assigned(decorator) and
  376. linestart then
  377. begin
  378. AsmWriteAnsiStringUnfiltered(decorator.LinePrefix);
  379. linestart:=false;
  380. end;
  381. end;
  382. procedure TExternalAssemblerOutputFile.MaybeAddLinePostfix;
  383. begin
  384. if assigned(decorator) and
  385. not linestart then
  386. begin
  387. AsmWriteAnsiStringUnfiltered(decorator.LinePostfix);
  388. linestart:=true;
  389. end;
  390. end;
  391. procedure TExternalAssemblerOutputFile.AsmWriteAnsiStringUnfiltered(const s: ansistring);
  392. var
  393. StartIndex, ToWrite: longint;
  394. begin
  395. if s='' then
  396. exit;
  397. if OutCnt+length(s)>=AsmOutSize then
  398. AsmFlush;
  399. StartIndex:=1;
  400. ToWrite:=length(s);
  401. while ToWrite>AsmOutSize do
  402. begin
  403. Move(s[StartIndex],OutBuf[OutCnt],AsmOutSize);
  404. inc(OutCnt,AsmOutSize);
  405. inc(AsmSize,AsmOutSize);
  406. AsmFlush;
  407. inc(StartIndex,AsmOutSize);
  408. dec(ToWrite,AsmOutSize);
  409. end;
  410. Move(s[StartIndex],OutBuf[OutCnt],ToWrite);
  411. inc(OutCnt,ToWrite);
  412. inc(AsmSize,ToWrite);
  413. end;
  414. constructor TExternalAssemblerOutputFile.Create(_owner: TExternalAssembler);
  415. begin
  416. owner:=_owner;
  417. linestart:=true;
  418. end;
  419. Procedure TExternalAssemblerOutputFile.AsmWrite(const c: char);
  420. begin
  421. if assigned(decorator) then
  422. AsmWriteFiltered(c)
  423. else
  424. begin
  425. if OutCnt+1>=AsmOutSize then
  426. AsmFlush;
  427. OutBuf[OutCnt]:=c;
  428. inc(OutCnt);
  429. inc(AsmSize);
  430. end;
  431. end;
  432. Procedure TExternalAssemblerOutputFile.AsmWrite(const s:string);
  433. begin
  434. if s='' then
  435. exit;
  436. if assigned(decorator) then
  437. AsmWriteFiltered(s)
  438. else
  439. begin
  440. if OutCnt+length(s)>=AsmOutSize then
  441. AsmFlush;
  442. Move(s[1],OutBuf[OutCnt],length(s));
  443. inc(OutCnt,length(s));
  444. inc(AsmSize,length(s));
  445. end;
  446. end;
  447. Procedure TExternalAssemblerOutputFile.AsmWrite(const s:ansistring);
  448. begin
  449. if s='' then
  450. exit;
  451. if assigned(decorator) then
  452. AsmWriteFiltered(s)
  453. else
  454. AsmWriteAnsiStringUnfiltered(s);
  455. end;
  456. procedure TExternalAssemblerOutputFile.AsmWriteLn(const c: char);
  457. begin
  458. AsmWrite(c);
  459. AsmLn;
  460. end;
  461. Procedure TExternalAssemblerOutputFile.AsmWriteLn(const s:string);
  462. begin
  463. AsmWrite(s);
  464. AsmLn;
  465. end;
  466. Procedure TExternalAssemblerOutputFile.AsmWriteLn(const s: ansistring);
  467. begin
  468. AsmWrite(s);
  469. AsmLn;
  470. end;
  471. Procedure TExternalAssemblerOutputFile.AsmWritePChar(p:pchar);
  472. var
  473. i,j : longint;
  474. begin
  475. i:=StrLen(p);
  476. if i=0 then
  477. exit;
  478. if assigned(decorator) then
  479. AsmWriteFiltered(p,i)
  480. else
  481. begin
  482. j:=i;
  483. while j>0 do
  484. begin
  485. i:=min(j,AsmOutSize);
  486. if OutCnt+i>=AsmOutSize then
  487. AsmFlush;
  488. Move(p[0],OutBuf[OutCnt],i);
  489. inc(OutCnt,i);
  490. inc(AsmSize,i);
  491. dec(j,i);
  492. p:=pchar(@p[i]);
  493. end;
  494. end;
  495. end;
  496. Procedure TExternalAssemblerOutputFile.AsmLn;
  497. var
  498. newline: pshortstring;
  499. newlineres: shortstring;
  500. index: longint;
  501. begin
  502. MaybeAddLinePostfix;
  503. if (cs_assemble_on_target in current_settings.globalswitches) then
  504. newline:=@target_info.newline
  505. else
  506. newline:=@source_info.newline;
  507. if assigned(decorator) then
  508. begin
  509. newlineres:=decorator.LineEnding(newline^);
  510. newline:=@newlineres;
  511. end;
  512. if OutCnt>=AsmOutSize-length(newline^) then
  513. AsmFlush;
  514. index:=1;
  515. repeat
  516. OutBuf[OutCnt]:=newline^[index];
  517. inc(OutCnt);
  518. inc(AsmSize);
  519. inc(index);
  520. until index>length(newline^);
  521. end;
  522. procedure TExternalAssemblerOutputFile.AsmCreate(Aplace:tcutplace);
  523. {$ifdef hasamiga}
  524. var
  525. tempFileName: TPathStr;
  526. {$endif}
  527. begin
  528. if owner.SmartAsm then
  529. owner.NextSmartName(Aplace);
  530. {$ifdef hasamiga}
  531. { on Amiga/MorphOS try to redirect .s files to the T: assign, which is
  532. for temp files, and usually (default setting) located in the RAM: drive.
  533. This highly improves assembling speed for complex projects like the
  534. compiler itself, especially on hardware with slow disk I/O.
  535. Consider this as a poor man's pipe on Amiga, because real pipe handling
  536. would be much more complex and error prone to implement. (KB) }
  537. if (([cs_asm_extern,cs_asm_leave,cs_assemble_on_target] * current_settings.globalswitches) = []) then
  538. begin
  539. { try to have an unique name for the .s file }
  540. tempFileName:=HexStr(GetProcessID shr 4,7)+ExtractFileName(owner.AsmFileName);
  541. {$ifndef morphos}
  542. { old Amiga RAM: handler only allows filenames up to 30 char }
  543. if Length(tempFileName) < 30 then
  544. {$endif}
  545. owner.AsmFileName:='T:'+tempFileName;
  546. end;
  547. {$endif}
  548. {$ifdef hasunix}
  549. if owner.DoPipe then
  550. begin
  551. if owner.SmartAsm then
  552. begin
  553. if (owner.SmartFilesCount<=1) then
  554. Message1(exec_i_assembling_smart,owner.name);
  555. end
  556. else
  557. Message1(exec_i_assembling_pipe,owner.AsmFileName);
  558. if checkverbosity(V_Executable) then
  559. comment(V_Executable,'Executing "'+maybequoted(owner.FindAssembler)+'" with command line "'+
  560. owner.MakeCmdLine+'"');
  561. POpen(outfile,maybequoted(owner.FindAssembler)+' '+owner.MakeCmdLine,'W');
  562. end
  563. else
  564. {$endif}
  565. begin
  566. Assign(outfile,owner.AsmFileName);
  567. {$push} {$I-}
  568. Rewrite(outfile,1);
  569. {$pop}
  570. if ioresult<>0 then
  571. begin
  572. fioerror:=true;
  573. Message1(exec_d_cant_create_asmfile,owner.AsmFileName);
  574. end;
  575. end;
  576. outcnt:=0;
  577. AsmSize:=0;
  578. AsmStartSize:=0;
  579. end;
  580. procedure TExternalAssemblerOutputFile.AsmClose;
  581. var
  582. f : file;
  583. FileAge : longint;
  584. begin
  585. AsmFlush;
  586. {$ifdef hasunix}
  587. if owner.DoPipe then
  588. begin
  589. if PClose(outfile) <> 0 then
  590. GenerateError;
  591. end
  592. else
  593. {$endif}
  594. begin
  595. {Touch Assembler time to ppu time is there is a ppufilename}
  596. if owner.ppufilename<>'' then
  597. begin
  598. Assign(f,owner.ppufilename);
  599. {$push} {$I-}
  600. reset(f,1);
  601. {$pop}
  602. if ioresult=0 then
  603. begin
  604. FileAge := FileGetDate(GetFileHandle(f));
  605. close(f);
  606. reset(outfile,1);
  607. FileSetDate(GetFileHandle(outFile),FileAge);
  608. end;
  609. end;
  610. close(outfile);
  611. end;
  612. end;
  613. {*****************************************************************************
  614. TExternalAssembler
  615. *****************************************************************************}
  616. function TExternalAssembler.single2str(d : single) : string;
  617. var
  618. hs : string;
  619. begin
  620. str(d,hs);
  621. { replace space with + }
  622. if hs[1]=' ' then
  623. hs[1]:='+';
  624. single2str:='0d'+hs
  625. end;
  626. function TExternalAssembler.double2str(d : double) : string;
  627. var
  628. hs : string;
  629. begin
  630. str(d,hs);
  631. { replace space with + }
  632. if hs[1]=' ' then
  633. hs[1]:='+';
  634. double2str:='0d'+hs
  635. end;
  636. function TExternalAssembler.extended2str(e : extended) : string;
  637. var
  638. hs : string;
  639. begin
  640. str(e,hs);
  641. { replace space with + }
  642. if hs[1]=' ' then
  643. hs[1]:='+';
  644. extended2str:='0d'+hs
  645. end;
  646. function TExternalAssembler.sleb128tostr(a: int64): string;
  647. var
  648. i,len : longint;
  649. buf : array[0..31] of byte;
  650. begin
  651. result:='';
  652. len:=EncodeSleb128(a,buf,0);
  653. for i:=0 to len-1 do
  654. begin
  655. if (i > 0) then
  656. result:=result+',';
  657. result:=result+tostr(buf[i]);
  658. end;
  659. end;
  660. function TExternalAssembler.uleb128tostr(a: qword): string;
  661. var
  662. i,len : longint;
  663. buf : array[0..31] of byte;
  664. begin
  665. result:='';
  666. len:=EncodeUleb128(a,buf,0);
  667. for i:=0 to len-1 do
  668. begin
  669. if (i > 0) then
  670. result:=result+',';
  671. result:=result+tostr(buf[i]);
  672. end;
  673. end;
  674. Function TExternalAssembler.DoPipe:boolean;
  675. begin
  676. {$ifdef hasunix}
  677. DoPipe:=(cs_asm_pipe in current_settings.globalswitches) and
  678. (([cs_asm_extern,cs_asm_leave,cs_assemble_on_target] * current_settings.globalswitches) = []) and
  679. ((asminfo^.id in [as_gas,as_ggas,as_darwin,as_powerpc_xcoff,as_clang_gas,as_clang_llvm,as_clang_llvm_darwin,as_solaris_as,as_clang_asdarwin]));
  680. {$else hasunix}
  681. DoPipe:=false;
  682. {$endif}
  683. end;
  684. function TExternalAssembler.CreateNewAsmWriter: TExternalAssemblerOutputFile;
  685. begin
  686. result:=TExternalAssemblerOutputFile.Create(self);
  687. end;
  688. Constructor TExternalAssembler.Create(info: pasminfo; smart: boolean);
  689. begin
  690. CreateWithWriter(info,CreateNewAsmWriter,true,smart);
  691. end;
  692. constructor TExternalAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter,smart: boolean);
  693. begin
  694. inherited Create(info,smart);
  695. fwriter:=wr;
  696. ffreewriter:=freewriter;
  697. if SmartAsm then
  698. begin
  699. path:=FixPath(ChangeFileExt(AsmFileName,target_info.smartext),false);
  700. CreateSmartLinkPath(path);
  701. end;
  702. end;
  703. procedure TExternalAssembler.CreateSmartLinkPath(const s:TPathStr);
  704. procedure DeleteFilesWithExt(const AExt:string);
  705. var
  706. dir : TRawByteSearchRec;
  707. begin
  708. if findfirst(FixPath(s,false)+'*'+AExt,faAnyFile,dir) = 0 then
  709. begin
  710. repeat
  711. DeleteFile(s+source_info.dirsep+dir.name);
  712. until findnext(dir) <> 0;
  713. end;
  714. findclose(dir);
  715. end;
  716. var
  717. hs : TPathStr;
  718. begin
  719. if PathExists(s,false) then
  720. begin
  721. { the path exists, now we clean only all the .o and .s files }
  722. DeleteFilesWithExt(target_info.objext);
  723. DeleteFilesWithExt(target_info.asmext);
  724. end
  725. else
  726. begin
  727. hs:=s;
  728. if hs[length(hs)] in ['/','\'] then
  729. delete(hs,length(hs),1);
  730. {$push} {$I-}
  731. mkdir(hs);
  732. {$pop}
  733. if ioresult<>0 then;
  734. end;
  735. end;
  736. const
  737. lastas : byte=255;
  738. var
  739. LastASBin : TCmdStr;
  740. Function TExternalAssembler.FindAssembler:string;
  741. var
  742. asfound : boolean;
  743. UtilExe : string;
  744. asmbin : TCmdStr;
  745. begin
  746. asfound:=false;
  747. asmbin:=asminfo^.asmbin;
  748. if (af_llvm in asminfo^.flags) then
  749. asmbin:=asmbin+llvmutilssuffix;
  750. if cs_assemble_on_target in current_settings.globalswitches then
  751. begin
  752. { If assembling on target, don't add any path PM }
  753. FindAssembler:=utilsprefix+ChangeFileExt(asmbin,target_info.exeext);
  754. exit;
  755. end
  756. else
  757. UtilExe:=utilsprefix+ChangeFileExt(asmbin,source_info.exeext);
  758. if lastas<>ord(asminfo^.id) then
  759. begin
  760. lastas:=ord(asminfo^.id);
  761. { is an assembler passed ? }
  762. if utilsdirectory<>'' then
  763. asfound:=FindFile(UtilExe,utilsdirectory,false,LastASBin);
  764. if not AsFound then
  765. asfound:=FindExe(UtilExe,false,LastASBin);
  766. if (not asfound) and not(cs_asm_extern in current_settings.globalswitches) then
  767. begin
  768. Message1(exec_e_assembler_not_found,LastASBin);
  769. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  770. end;
  771. if asfound then
  772. Message1(exec_t_using_assembler,LastASBin);
  773. end;
  774. FindAssembler:=LastASBin;
  775. end;
  776. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  777. var
  778. DosExitCode : Integer;
  779. begin
  780. result:=true;
  781. if (cs_asm_extern in current_settings.globalswitches) then
  782. begin
  783. if SmartAsm then
  784. AsmRes.AddAsmCommand(command,para,Name+'('+TosTr(SmartFilesCount)+')')
  785. else
  786. AsmRes.AddAsmCommand(command,para,name);
  787. exit;
  788. end;
  789. try
  790. FlushOutput;
  791. DosExitCode:=RequotedExecuteProcess(command,para);
  792. if DosExitCode<>0
  793. then begin
  794. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  795. result:=false;
  796. end;
  797. except on E:EOSError do
  798. begin
  799. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  800. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  801. result:=false;
  802. end;
  803. end;
  804. end;
  805. Function TExternalAssembler.DoAssemble:boolean;
  806. begin
  807. result:=true;
  808. if DoPipe then
  809. exit;
  810. if not(cs_asm_extern in current_settings.globalswitches) then
  811. begin
  812. if SmartAsm then
  813. begin
  814. if (SmartFilesCount<=1) then
  815. Message1(exec_i_assembling_smart,name);
  816. end
  817. else
  818. Message1(exec_i_assembling,name);
  819. end;
  820. repeat
  821. result:=CallAssembler(FindAssembler,MakeCmdLine)
  822. until not(result) or not RerunAssembler;
  823. if result then
  824. writer.RemoveAsm
  825. else
  826. GenerateError;
  827. end;
  828. function TExternalAssembler.MakeCmdLine: TCmdStr;
  829. function section_high_bound:longint;
  830. var
  831. alt : tasmlisttype;
  832. begin
  833. result:=0;
  834. for alt:=low(tasmlisttype) to high(tasmlisttype) do
  835. result:=result+current_asmdata.asmlists[alt].section_count;
  836. end;
  837. const
  838. min_big_obj_section_count = $7fff;
  839. begin
  840. result:=asminfo^.asmcmd;
  841. if af_llvm in target_asm.flags then
  842. Replace(result,'$TRIPLET',targettriplet(triplet_llvm))
  843. {$ifdef arm}
  844. else if (target_info.system=system_arm_ios) then
  845. Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]))
  846. {$endif arm}
  847. ;
  848. if (cs_assemble_on_target in current_settings.globalswitches) then
  849. begin
  850. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  851. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  852. end
  853. else
  854. begin
  855. {$ifdef hasunix}
  856. if DoPipe then
  857. if not(asminfo^.id in [as_clang_gas,as_clang_asdarwin,as_clang_llvm,as_clang_llvm_darwin]) then
  858. Replace(result,'$ASM','')
  859. else
  860. Replace(result,'$ASM','-')
  861. else
  862. {$endif}
  863. Replace(result,'$ASM',maybequoted(AsmFileName));
  864. Replace(result,'$OBJ',maybequoted(ObjFileName));
  865. end;
  866. if (cs_create_pic in current_settings.moduleswitches) then
  867. Replace(result,'$PIC','-KPIC')
  868. else
  869. Replace(result,'$PIC','');
  870. if (cs_asm_source in current_settings.globalswitches) then
  871. Replace(result,'$NOWARN','')
  872. else
  873. Replace(result,'$NOWARN','-W');
  874. if target_info.endian=endian_little then
  875. Replace(result,'$ENDIAN','-mlittle')
  876. else
  877. Replace(result,'$ENDIAN','-mbig');
  878. { as we don't keep track of the amount of sections we created we simply
  879. enable Big Obj COFF files always for targets that need them }
  880. if (cs_asm_pre_binutils_2_25 in current_settings.globalswitches) or
  881. not (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) or
  882. (section_high_bound<min_big_obj_section_count) then
  883. Replace(result,'$BIGOBJ','')
  884. else
  885. Replace(result,'$BIGOBJ','-mbig-obj');
  886. Replace(result,'$EXTRAOPT',asmextraopt);
  887. end;
  888. function TExternalAssembler.RerunAssembler: boolean;
  889. begin
  890. result:=false;
  891. end;
  892. procedure TExternalAssembler.ResetSourceLines;
  893. procedure DoReset(f:tinputfile);
  894. var
  895. i : longint;
  896. begin
  897. if not assigned(f) then
  898. exit;
  899. for i:=0 to f.maxlinebuf-1 do
  900. if f.linebuf^[i]<0 then
  901. f.linebuf^[i]:=-f.linebuf^[i]-1;
  902. end;
  903. begin
  904. DoReset(infile);
  905. DoReset(lastinfile);
  906. end;
  907. procedure TExternalAssembler.WriteSourceLine(hp: tailineinfo);
  908. var
  909. module : tmodule;
  910. begin
  911. { load infile }
  912. if (lastfileinfo.moduleindex<>hp.fileinfo.moduleindex) or
  913. (lastfileinfo.fileindex<>hp.fileinfo.fileindex) then
  914. begin
  915. { in case of a generic the module can be different }
  916. if current_module.unit_index=hp.fileinfo.moduleindex then
  917. module:=current_module
  918. else
  919. module:=get_module(hp.fileinfo.moduleindex);
  920. { during the compilation of the system unit there are cases when
  921. the fileinfo contains just zeros => invalid }
  922. if assigned(module) then
  923. infile:=module.sourcefiles.get_file(hp.fileinfo.fileindex)
  924. else
  925. infile:=nil;
  926. if assigned(infile) then
  927. begin
  928. { open only if needed !! }
  929. if (cs_asm_source in current_settings.globalswitches) then
  930. infile.open;
  931. end;
  932. { avoid unnecessary reopens of the same file !! }
  933. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  934. lastfileinfo.moduleindex:=hp.fileinfo.moduleindex;
  935. { be sure to change line !! }
  936. lastfileinfo.line:=-1;
  937. end;
  938. { write source }
  939. if (cs_asm_source in current_settings.globalswitches) and
  940. assigned(infile) then
  941. begin
  942. if (infile<>lastinfile) then
  943. begin
  944. writer.AsmWriteLn(asminfo^.comment+'['+infile.name+']');
  945. if assigned(lastinfile) then
  946. lastinfile.close;
  947. end;
  948. if (hp.fileinfo.line<>lastfileinfo.line) and
  949. (hp.fileinfo.line<infile.maxlinebuf) then
  950. begin
  951. if (hp.fileinfo.line<>0) and
  952. (infile.linebuf^[hp.fileinfo.line]>=0) then
  953. writer.AsmWriteLn(asminfo^.comment+'['+tostr(hp.fileinfo.line)+'] '+
  954. fixline(infile.GetLineStr(hp.fileinfo.line)));
  955. { set it to a negative value !
  956. to make that is has been read already !! PM }
  957. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  958. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  959. end;
  960. end;
  961. lastfileinfo:=hp.fileinfo;
  962. lastinfile:=infile;
  963. end;
  964. procedure TExternalAssembler.WriteTempalloc(hp: tai_tempalloc);
  965. begin
  966. {$ifdef EXTDEBUG}
  967. if assigned(hp.problem) then
  968. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  969. tostr(hp.tempsize)+' '+hp.problem^)
  970. else
  971. {$endif EXTDEBUG}
  972. writer.AsmWriteLn(asminfo^.comment+'Temp '+tostr(hp.temppos)+','+
  973. tostr(hp.tempsize)+' '+tempallocstr[hp.allocation]);
  974. end;
  975. procedure TExternalAssembler.WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  976. var
  977. pdata: pbyte;
  978. index, step, swapmask, count: longint;
  979. ssingle: single;
  980. ddouble: double;
  981. {$ifdef FPC_COMP_IS_INT64}
  982. ccomp: int64;
  983. {$else}
  984. ccomp: comp;
  985. {$endif}
  986. comp_data_size : byte;
  987. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  988. eextended: extended;
  989. {$else}
  990. {$ifdef FPC_SOFT_FPUX80}
  991. {$define USE_SOFT_FLOATX80}
  992. f32 : float32;
  993. f64 : float64;
  994. eextended: floatx80;
  995. gap_ofs_low,gap_ofs_high : byte;
  996. gap_index, gap_size : byte;
  997. has_gap : boolean;
  998. {$endif}
  999. {$endif cpuextended}
  1000. begin
  1001. {$ifdef USE_SOFT_FLOATX80}
  1002. has_gap:=false;
  1003. {$endif USE_SOFT_FLOATX80}
  1004. if do_line then
  1005. begin
  1006. case tai_realconst(hp).realtyp of
  1007. aitrealconst_s32bit:
  1008. writer.AsmWriteLn(asminfo^.comment+'s32bit real value: '+single2str(tai_realconst(hp).value.s32val));
  1009. aitrealconst_s64bit:
  1010. writer.AsmWriteLn(asminfo^.comment+'s64bit real value: '+double2str(tai_realconst(hp).value.s64val));
  1011. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1012. { can't write full 80 bit floating point constants yet on non-x86 }
  1013. aitrealconst_s80bit:
  1014. writer.AsmWriteLn(asminfo^.comment+'s80bit real value: '+extended2str(tai_realconst(hp).value.s80val));
  1015. {$else}
  1016. {$ifdef USE_SOFT_FLOATX80}
  1017. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1018. aitrealconst_s80bit:
  1019. begin
  1020. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1021. writer.AsmWriteLn(asminfo^.comment+'Emulated s80bit real value (on s64bit): '+double2str(tai_realconst(hp).value.s80val))
  1022. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1023. writer.AsmWriteLn(asminfo^.comment+'Emulated s80bit real value (on s32bit): '+single2str(tai_realconst(hp).value.s80val))
  1024. else
  1025. internalerror(2017091901);
  1026. end;
  1027. {$pop}
  1028. {$endif}
  1029. {$endif cpuextended}
  1030. aitrealconst_s64comp:
  1031. begin
  1032. writer.AsmWriteLn(asminfo^.comment+'s64comp real value: '+extended2str(tai_realconst(hp).value.s64compval));
  1033. comp_data_size:=sizeof(comp);
  1034. if (comp_data_size<>tai_realconst(hp).datasize) then
  1035. writer.AsmWriteLn(asminfo^.comment+'s64comp value type size is '+tostr(comp_data_size)+' but datasize is '+tostr(tai_realconst(hp).datasize));
  1036. end
  1037. else
  1038. internalerror(2014050604);
  1039. end;
  1040. end;
  1041. writer.AsmWrite(dbdir);
  1042. { generic float writing code: get start address of value, then write
  1043. byte by byte. Can't use fields directly, because e.g ts64comp is
  1044. defined as extended on x86 }
  1045. case tai_realconst(hp).realtyp of
  1046. aitrealconst_s32bit:
  1047. begin
  1048. ssingle:=single(tai_realconst(hp).value.s32val);
  1049. pdata:=@ssingle;
  1050. end;
  1051. aitrealconst_s64bit:
  1052. begin
  1053. ddouble:=double(tai_realconst(hp).value.s64val);
  1054. pdata:=@ddouble;
  1055. end;
  1056. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1057. { can't write full 80 bit floating point constants yet on non-x86 }
  1058. aitrealconst_s80bit:
  1059. begin
  1060. eextended:=extended(tai_realconst(hp).value.s80val);
  1061. pdata:=@eextended;
  1062. end;
  1063. {$else}
  1064. {$ifdef USE_SOFT_FLOATX80}
  1065. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1066. aitrealconst_s80bit:
  1067. begin
  1068. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1069. begin
  1070. f64:=float64(double(tai_realconst(hp).value.s80val));
  1071. if float64_is_signaling_nan(f64)<>0 then
  1072. begin
  1073. f64.low := 0;
  1074. f64.high := longword($fff80000);
  1075. end;
  1076. eextended:=float64_to_floatx80(f64);
  1077. end
  1078. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1079. begin
  1080. f32:=float32(single(tai_realconst(hp).value.s80val));
  1081. if float32_is_signaling_nan(f32)<>0 then
  1082. begin
  1083. f32 := longword($ffc00000);
  1084. end;
  1085. eextended:=float32_to_floatx80(f32);
  1086. end
  1087. else
  1088. internalerror(2017091902);
  1089. pdata:=@eextended;
  1090. if sizeof(eextended)>10 then
  1091. begin
  1092. gap_ofs_high:=(pbyte(@eextended.high) - pbyte(@eextended));
  1093. gap_ofs_low:=(pbyte(@eextended.low) - pbyte(@eextended));
  1094. if (gap_ofs_low<gap_ofs_high) then
  1095. begin
  1096. gap_index:=gap_ofs_low+sizeof(eextended.low);
  1097. gap_size:=gap_ofs_high-gap_index;
  1098. end
  1099. else
  1100. begin
  1101. gap_index:=gap_ofs_high+sizeof(eextended.high);
  1102. gap_size:=gap_ofs_low-gap_index;
  1103. end;
  1104. if source_info.endian<>target_info.endian then
  1105. gap_index:=gap_index+gap_size-1;
  1106. has_gap:=gap_size <> 0;
  1107. end
  1108. else
  1109. has_gap:=false;
  1110. end;
  1111. {$pop}
  1112. {$endif}
  1113. {$endif cpuextended}
  1114. aitrealconst_s64comp:
  1115. begin
  1116. {$ifdef FPC_COMP_IS_INT64}
  1117. ccomp:=system.trunc(tai_realconst(hp).value.s64compval);
  1118. {$else}
  1119. ccomp:=comp(tai_realconst(hp).value.s64compval);
  1120. {$endif}
  1121. pdata:=@ccomp;
  1122. end;
  1123. else
  1124. internalerror(2014051001);
  1125. end;
  1126. count:=tai_realconst(hp).datasize;
  1127. { write bytes in inverse order if source and target endianess don't
  1128. match }
  1129. if source_info.endian<>target_info.endian then
  1130. begin
  1131. { go from back to front }
  1132. {$ifdef USE_SOFT_FLOATX80}
  1133. if has_gap then
  1134. index:=sizeof(eextended)-1
  1135. else
  1136. {$endif USE_SOFT_FLOATX80}
  1137. index:=count-1;
  1138. step:=-1;
  1139. end
  1140. else
  1141. begin
  1142. index:=0;
  1143. step:=1;
  1144. end;
  1145. {$ifdef ARM}
  1146. { ARM-specific: low and high dwords of a double may be swapped }
  1147. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  1148. begin
  1149. { only supported for double }
  1150. if tai_realconst(hp).datasize<>8 then
  1151. internalerror(2014050605);
  1152. { switch bit of the index so that the words are written in
  1153. the opposite order }
  1154. swapmask:=4;
  1155. end
  1156. else
  1157. {$endif ARM}
  1158. swapmask:=0;
  1159. repeat
  1160. {$ifdef USE_SOFT_FLOATX80}
  1161. if has_gap and (index=gap_index) then
  1162. index:=index+step*gap_size;
  1163. {$endif USE_SOFT_FLOATX80}
  1164. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  1165. inc(index,step);
  1166. dec(count);
  1167. if count<>0 then
  1168. writer.AsmWrite(',');
  1169. until count=0;
  1170. { padding }
  1171. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  1172. writer.AsmWrite(',0');
  1173. writer.AsmLn;
  1174. end;
  1175. function TExternalAssembler.WriteComments(var hp: tai): boolean;
  1176. begin
  1177. result:=true;
  1178. case hp.typ of
  1179. ait_comment :
  1180. Begin
  1181. writer.AsmWrite(asminfo^.comment);
  1182. writer.AsmWritePChar(tai_comment(hp).str);
  1183. writer.AsmLn;
  1184. End;
  1185. ait_regalloc :
  1186. begin
  1187. if (cs_asm_regalloc in current_settings.globalswitches) then
  1188. begin
  1189. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  1190. repeat
  1191. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  1192. if (hp.next=nil) or
  1193. (tai(hp.next).typ<>ait_regalloc) or
  1194. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  1195. break;
  1196. hp:=tai(hp.next);
  1197. writer.AsmWrite(',');
  1198. until false;
  1199. writer.AsmWrite(' ');
  1200. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  1201. end;
  1202. end;
  1203. ait_tempalloc :
  1204. begin
  1205. if (cs_asm_tempalloc in current_settings.globalswitches) then
  1206. WriteTempalloc(tai_tempalloc(hp));
  1207. end;
  1208. ait_varloc:
  1209. begin
  1210. { ait_varloc is present here only when register allocation is not done ( -sr option ) }
  1211. if tai_varloc(hp).newlocationhi<>NR_NO then
  1212. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1213. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1214. else
  1215. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1216. std_regname(tai_varloc(hp).newlocation));
  1217. end;
  1218. else
  1219. result:=false;
  1220. end;
  1221. end;
  1222. procedure TExternalAssembler.WriteTree(p:TAsmList);
  1223. begin
  1224. end;
  1225. procedure TExternalAssembler.WriteAsmList;
  1226. begin
  1227. end;
  1228. procedure TExternalAssembler.MakeObject;
  1229. begin
  1230. writer.AsmCreate(cut_normal);
  1231. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  1232. lastfileinfo.line := -1;
  1233. lastinfile := nil;
  1234. lastsectype := sec_none;
  1235. WriteAsmList;
  1236. writer.AsmClose;
  1237. if not(writer.ioerror) then
  1238. DoAssemble;
  1239. end;
  1240. destructor TExternalAssembler.Destroy;
  1241. begin
  1242. if ffreewriter then
  1243. writer.Free;
  1244. inherited;
  1245. end;
  1246. {*****************************************************************************
  1247. TInternalAssembler
  1248. *****************************************************************************}
  1249. constructor TInternalAssembler.Create(info: pasminfo; smart: boolean);
  1250. begin
  1251. inherited;
  1252. ObjOutput:=nil;
  1253. ObjData:=nil;
  1254. SmartAsm:=smart;
  1255. {$ifdef ARM}
  1256. Code16:=current_settings.instructionset=is_thumb;
  1257. {$endif ARM}
  1258. end;
  1259. destructor TInternalAssembler.destroy;
  1260. begin
  1261. if assigned(ObjData) then
  1262. ObjData.free;
  1263. if assigned(ObjOutput) then
  1264. ObjOutput.free;
  1265. end;
  1266. procedure TInternalAssembler.WriteStab(p:pchar);
  1267. function consumecomma(var p:pchar):boolean;
  1268. begin
  1269. while (p^=' ') do
  1270. inc(p);
  1271. result:=(p^=',');
  1272. inc(p);
  1273. end;
  1274. function consumenumber(var p:pchar;out value:longint):boolean;
  1275. var
  1276. hs : string;
  1277. len,
  1278. code : integer;
  1279. begin
  1280. value:=0;
  1281. while (p^=' ') do
  1282. inc(p);
  1283. len:=0;
  1284. while (p^ in ['0'..'9']) do
  1285. begin
  1286. inc(len);
  1287. hs[len]:=p^;
  1288. inc(p);
  1289. end;
  1290. if len>0 then
  1291. begin
  1292. hs[0]:=chr(len);
  1293. val(hs,value,code);
  1294. end
  1295. else
  1296. code:=-1;
  1297. result:=(code=0);
  1298. end;
  1299. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  1300. var
  1301. hs : string;
  1302. len,
  1303. code : integer;
  1304. pstart : pchar;
  1305. sym : tobjsymbol;
  1306. exprvalue : longint;
  1307. gotmin,
  1308. have_first_symbol,
  1309. have_second_symbol,
  1310. dosub : boolean;
  1311. begin
  1312. result:=false;
  1313. value:=0;
  1314. relocsym:=nil;
  1315. gotmin:=false;
  1316. have_first_symbol:=false;
  1317. have_second_symbol:=false;
  1318. repeat
  1319. dosub:=false;
  1320. exprvalue:=0;
  1321. if gotmin then
  1322. begin
  1323. dosub:=true;
  1324. gotmin:=false;
  1325. end;
  1326. while (p^=' ') do
  1327. inc(p);
  1328. case p^ of
  1329. #0 :
  1330. break;
  1331. ' ' :
  1332. inc(p);
  1333. '0'..'9' :
  1334. begin
  1335. len:=0;
  1336. while (p^ in ['0'..'9']) do
  1337. begin
  1338. inc(len);
  1339. hs[len]:=p^;
  1340. inc(p);
  1341. end;
  1342. hs[0]:=chr(len);
  1343. val(hs,exprvalue,code);
  1344. if code<>0 then
  1345. internalerror(200702251);
  1346. end;
  1347. '.','_',
  1348. 'A'..'Z',
  1349. 'a'..'z' :
  1350. begin
  1351. pstart:=p;
  1352. while not(p^ in [#0,' ','-','+']) do
  1353. inc(p);
  1354. len:=p-pstart;
  1355. if len>255 then
  1356. internalerror(200509187);
  1357. hs[0]:=chr(len);
  1358. move(pstart^,hs[1],len);
  1359. sym:=objdata.symbolref(hs);
  1360. { Second symbol? }
  1361. if assigned(relocsym) then
  1362. begin
  1363. if have_second_symbol then
  1364. internalerror(2007032201);
  1365. have_second_symbol:=true;
  1366. if not have_first_symbol then
  1367. internalerror(2007032202);
  1368. { second symbol should substracted to first }
  1369. if not dosub then
  1370. internalerror(2007032203);
  1371. if (relocsym.objsection<>sym.objsection) then
  1372. internalerror(2005091810);
  1373. exprvalue:=relocsym.address-sym.address;
  1374. relocsym:=nil;
  1375. dosub:=false;
  1376. end
  1377. else
  1378. begin
  1379. relocsym:=sym;
  1380. if assigned(sym.objsection) then
  1381. begin
  1382. { first symbol should be + }
  1383. if not have_first_symbol and dosub then
  1384. internalerror(2007032204);
  1385. have_first_symbol:=true;
  1386. end;
  1387. end;
  1388. end;
  1389. '+' :
  1390. begin
  1391. { nothing, by default addition is done }
  1392. inc(p);
  1393. end;
  1394. '-' :
  1395. begin
  1396. gotmin:=true;
  1397. inc(p);
  1398. end;
  1399. else
  1400. internalerror(200509189);
  1401. end;
  1402. if dosub then
  1403. dec(value,exprvalue)
  1404. else
  1405. inc(value,exprvalue);
  1406. until false;
  1407. result:=true;
  1408. end;
  1409. var
  1410. stabstrlen,
  1411. ofs,
  1412. nline,
  1413. nidx,
  1414. nother,
  1415. i : longint;
  1416. stab : TObjStabEntry;
  1417. relocsym : TObjSymbol;
  1418. pstr,
  1419. pcurr,
  1420. pendquote : pchar;
  1421. oldsec : TObjSection;
  1422. begin
  1423. pcurr:=nil;
  1424. pstr:=nil;
  1425. pendquote:=nil;
  1426. relocsym:=nil;
  1427. ofs:=0;
  1428. { Parse string part }
  1429. if (p[0]='"') then
  1430. begin
  1431. pstr:=@p[1];
  1432. { Ignore \" inside the string }
  1433. i:=1;
  1434. while not((p[i]='"') and (p[i-1]<>'\')) and
  1435. (p[i]<>#0) do
  1436. inc(i);
  1437. pendquote:=@p[i];
  1438. pendquote^:=#0;
  1439. pcurr:=@p[i+1];
  1440. if not consumecomma(pcurr) then
  1441. internalerror(200509181);
  1442. end
  1443. else
  1444. pcurr:=p;
  1445. { When in pass 1 then only alloc and leave }
  1446. if ObjData.currpass=1 then
  1447. begin
  1448. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  1449. if assigned(pstr) and (pstr[0]<>#0) then
  1450. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  1451. end
  1452. else
  1453. begin
  1454. { Stabs format: nidx,nother,nline[,offset] }
  1455. if not consumenumber(pcurr,nidx) then
  1456. internalerror(200509182);
  1457. if not consumecomma(pcurr) then
  1458. internalerror(200509183);
  1459. if not consumenumber(pcurr,nother) then
  1460. internalerror(200509184);
  1461. if not consumecomma(pcurr) then
  1462. internalerror(200509185);
  1463. if not consumenumber(pcurr,nline) then
  1464. internalerror(200509186);
  1465. if consumecomma(pcurr) then
  1466. consumeoffset(pcurr,relocsym,ofs);
  1467. { Generate stab entry }
  1468. if assigned(pstr) and (pstr[0]<>#0) then
  1469. begin
  1470. stabstrlen:=strlen(pstr);
  1471. {$ifdef optimizestabs}
  1472. StabStrEntry:=nil;
  1473. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  1474. begin
  1475. hs:=strpas(pstr);
  1476. StabstrEntry:=StabStrDict.Find(hs);
  1477. if not assigned(StabstrEntry) then
  1478. begin
  1479. StabstrEntry:=TStabStrEntry.Create(hs);
  1480. StabstrEntry:=StabStrSec.Size;
  1481. StabStrDict.Insert(StabstrEntry);
  1482. { generate new stab }
  1483. StabstrEntry:=nil;
  1484. end;
  1485. end;
  1486. if assigned(StabstrEntry) then
  1487. stab.strpos:=StabstrEntry.strpos
  1488. else
  1489. {$endif optimizestabs}
  1490. begin
  1491. stab.strpos:=ObjData.StabStrSec.Size;
  1492. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  1493. end;
  1494. end
  1495. else
  1496. stab.strpos:=0;
  1497. stab.ntype:=byte(nidx);
  1498. stab.ndesc:=word(nline);
  1499. stab.nother:=byte(nother);
  1500. stab.nvalue:=ofs;
  1501. { Write the stab first without the value field. Then
  1502. write a the value field with relocation }
  1503. oldsec:=ObjData.CurrObjSec;
  1504. ObjData.SetSection(ObjData.StabsSec);
  1505. MaybeSwapStab(stab);
  1506. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  1507. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  1508. ObjData.setsection(oldsec);
  1509. end;
  1510. if assigned(pendquote) then
  1511. pendquote^:='"';
  1512. end;
  1513. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  1514. begin
  1515. { maybe end of list }
  1516. while not assigned(hp) do
  1517. begin
  1518. if currlistidx<lists then
  1519. begin
  1520. inc(currlistidx);
  1521. currlist:=list[currlistidx];
  1522. hp:=Tai(currList.first);
  1523. end
  1524. else
  1525. begin
  1526. MaybeNextList:=false;
  1527. exit;
  1528. end;
  1529. end;
  1530. MaybeNextList:=true;
  1531. end;
  1532. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  1533. var
  1534. objsym : TObjSymbol;
  1535. indsym : TObjSymbol;
  1536. begin
  1537. Result:=
  1538. Assigned(hp) and
  1539. (hp.typ=ait_symbol);
  1540. if not Result then
  1541. Exit;
  1542. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1543. objsym.size:=0;
  1544. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1545. if not Assigned(indsym) then
  1546. begin
  1547. { it's possible that indirect symbol is not present in the list,
  1548. so we must create it as undefined }
  1549. indsym:=ObjData.CObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1550. indsym.typ:=AT_NONE;
  1551. indsym.bind:=AB_NONE;
  1552. end;
  1553. objsym.indsymbol:=indsym;
  1554. Result:=true;
  1555. end;
  1556. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1557. var
  1558. objsym,
  1559. objsymend : TObjSymbol;
  1560. cpu: tcputype;
  1561. eabi_section, TmpSection: TObjSection;
  1562. begin
  1563. while assigned(hp) do
  1564. begin
  1565. {$ifdef DEBUG}
  1566. if not(hp.typ in SkipLineInfo) then
  1567. current_filepos:=tailineinfo(hp).fileinfo;
  1568. {$endif DEBUG}
  1569. case hp.typ of
  1570. ait_align :
  1571. begin
  1572. if tai_align_abstract(hp).aligntype>1 then
  1573. begin
  1574. { always use the maximum fillsize in this pass to avoid possible
  1575. short jumps to become out of range }
  1576. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1577. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1578. { may need to increase alignment of section }
  1579. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1580. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1581. end
  1582. else
  1583. Tai_align_abstract(hp).fillsize:=0;
  1584. end;
  1585. ait_datablock :
  1586. begin
  1587. {$ifdef USE_COMM_IN_BSS}
  1588. if writingpackages and
  1589. Tai_datablock(hp).is_global then
  1590. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1591. else
  1592. {$endif USE_COMM_IN_BSS}
  1593. begin
  1594. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1595. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1596. ObjData.alloc(Tai_datablock(hp).size);
  1597. end;
  1598. end;
  1599. ait_realconst:
  1600. ObjData.alloc(tai_realconst(hp).savesize);
  1601. ait_const:
  1602. begin
  1603. { if symbols are provided we can calculate the value for relative symbols.
  1604. This is required for length calculation of leb128 constants }
  1605. if assigned(tai_const(hp).sym) then
  1606. begin
  1607. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1608. { objsym already defined and there is endsym? }
  1609. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1610. begin
  1611. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1612. { objsymend already defined? }
  1613. if assigned(objsymend.objsection) then
  1614. begin
  1615. if objsymend.objsection<>objsym.objsection then
  1616. begin
  1617. { leb128 relative constants are not relocatable, but other types are,
  1618. given that objsym belongs to the current section. }
  1619. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1620. (objsym.objsection<>ObjData.CurrObjSec) then
  1621. InternalError(200404124);
  1622. end
  1623. {$push} {$R-}{$Q-}
  1624. else
  1625. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1626. end;
  1627. {$pop}
  1628. end;
  1629. end;
  1630. ObjData.alloc(tai_const(hp).size);
  1631. end;
  1632. ait_directive:
  1633. begin
  1634. case tai_directive(hp).directive of
  1635. asd_indirect_symbol:
  1636. { handled in TreePass1 }
  1637. ;
  1638. asd_lazy_reference:
  1639. begin
  1640. if tai_directive(hp).name='' then
  1641. Internalerror(2009112101);
  1642. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1643. objsym.bind:=AB_LAZY;
  1644. end;
  1645. asd_reference:
  1646. { ignore for now, but should be added}
  1647. ;
  1648. asd_cpu:
  1649. begin
  1650. ObjData.CPUType:=cpu_none;
  1651. for cpu:=low(tcputype) to high(tcputype) do
  1652. if cputypestr[cpu]=tai_directive(hp).name then
  1653. begin
  1654. ObjData.CPUType:=cpu;
  1655. break;
  1656. end;
  1657. end;
  1658. asd_weak_definition:
  1659. begin
  1660. if tai_directive(hp).name='' then
  1661. Internalerror(2022040901);
  1662. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1663. objsym.bind:=AB_WEAK;
  1664. end;
  1665. {$ifdef OMFOBJSUPPORT}
  1666. asd_omf_linnum_line:
  1667. { ignore for now, but should be added}
  1668. ;
  1669. {$endif OMFOBJSUPPORT}
  1670. {$ifdef ARM}
  1671. asd_thumb_func:
  1672. ObjData.ThumbFunc:=true;
  1673. asd_force_thumb:
  1674. begin
  1675. ObjData.ThumbFunc:=true;
  1676. Code16:=true;
  1677. end;
  1678. asd_code:
  1679. begin
  1680. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1681. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1682. Code16:=tai_directive(hp).name='16';
  1683. end
  1684. {$endif ARM}
  1685. {$ifdef RISCV}
  1686. asd_option:
  1687. internalerror(2019031701);
  1688. {$endif RISCV}
  1689. else
  1690. internalerror(2010011101);
  1691. end;
  1692. end;
  1693. ait_section:
  1694. begin
  1695. if Tai_section(hp).sectype=sec_user then
  1696. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).secflags,Tai_section(hp).secprogbits,Tai_section(hp).name^,Tai_section(hp).secorder)
  1697. else
  1698. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1699. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1700. end;
  1701. ait_symbol :
  1702. begin
  1703. { needs extra support in the internal assembler }
  1704. { the value is just ignored }
  1705. {if tai_symbol(hp).has_value then
  1706. internalerror(2009090804); ;}
  1707. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1708. end;
  1709. ait_symbolpair :
  1710. with tai_symbolpair(hp) do
  1711. ObjData.SymbolPairDefine(kind,sym^,value^);
  1712. ait_label :
  1713. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1714. ait_string :
  1715. ObjData.alloc(Tai_string(hp).len);
  1716. ait_instruction :
  1717. begin
  1718. {$ifdef arm}
  1719. if code16 then
  1720. include(taicpu(hp).flags,cf_thumb)
  1721. else
  1722. exclude(taicpu(hp).flags,cf_thumb);
  1723. {$endif arm}
  1724. { reset instructions which could change in pass 2 }
  1725. Taicpu(hp).resetpass2;
  1726. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1727. end;
  1728. ait_cutobject :
  1729. if SmartAsm then
  1730. break;
  1731. ait_eabi_attribute :
  1732. begin
  1733. eabi_section:=ObjData.findsection('.ARM.attributes');
  1734. if not(assigned(eabi_section)) then
  1735. begin
  1736. TmpSection:=ObjData.CurrObjSec;
  1737. ObjData.CreateSection(sec_arm_attribute,[],SPB_ARM_ATTRIBUTES,'',secorder_default);
  1738. eabi_section:=ObjData.CurrObjSec;
  1739. ObjData.setsection(TmpSection);
  1740. end;
  1741. if eabi_section.Size=0 then
  1742. eabi_section.alloc(16);
  1743. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1744. case tai_eabi_attribute(hp).eattr_typ of
  1745. eattrtype_dword:
  1746. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1747. eattrtype_ntbs:
  1748. if assigned(tai_eabi_attribute(hp).valuestr) then
  1749. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1750. else
  1751. eabi_section.alloc(1);
  1752. else
  1753. Internalerror(2019100701);
  1754. end;
  1755. end;
  1756. {$ifdef WASM}
  1757. ait_globaltype:
  1758. TWasmObjData(ObjData).DeclareGlobalType(tai_globaltype(hp));
  1759. ait_functype:
  1760. TWasmObjData(ObjData).DeclareFuncType(tai_functype(hp));
  1761. ait_tagtype:
  1762. TWasmObjData(ObjData).DeclareTagType(tai_tagtype(hp));
  1763. ait_export_name:
  1764. TWasmObjData(ObjData).DeclareExportName(tai_export_name(hp));
  1765. ait_import_module:
  1766. TWasmObjData(ObjData).DeclareImportModule(tai_import_module(hp));
  1767. ait_import_name:
  1768. TWasmObjData(ObjData).DeclareImportName(tai_import_name(hp));
  1769. ait_local:
  1770. TWasmObjData(ObjData).DeclareLocal(tai_local(hp));
  1771. {$endif WASM}
  1772. else
  1773. ;
  1774. end;
  1775. hp:=Tai(hp.next);
  1776. end;
  1777. TreePass0:=hp;
  1778. end;
  1779. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1780. var
  1781. objsym,
  1782. objsymend : TObjSymbol;
  1783. cpu: tcputype;
  1784. eabi_section: TObjSection;
  1785. begin
  1786. while assigned(hp) do
  1787. begin
  1788. {$ifdef DEBUG}
  1789. if not(hp.typ in SkipLineInfo) then
  1790. current_filepos:=tailineinfo(hp).fileinfo;
  1791. {$endif DEBUG}
  1792. case hp.typ of
  1793. ait_align :
  1794. begin
  1795. if tai_align_abstract(hp).aligntype>1 then
  1796. begin
  1797. { here we must determine the fillsize which is used in pass2 }
  1798. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1799. ObjData.CurrObjSec.Size;
  1800. { maximum number of bytes for alignment exeeded? }
  1801. if (Tai_align_abstract(hp).aligntype<>Tai_align_abstract(hp).maxbytes) and
  1802. (Tai_align_abstract(hp).fillsize>Tai_align_abstract(hp).maxbytes) then
  1803. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Byte(Tai_align_abstract(hp).aligntype div 2))-
  1804. ObjData.CurrObjSec.Size;
  1805. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1806. end;
  1807. end;
  1808. ait_datablock :
  1809. begin
  1810. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1811. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1812. Message(asmw_e_alloc_data_only_in_bss);
  1813. {$ifdef USE_COMM_IN_BSS}
  1814. if writingpackages and
  1815. Tai_datablock(hp).is_global then
  1816. begin
  1817. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1818. objsym.size:=Tai_datablock(hp).size;
  1819. objsym.bind:=AB_COMMON;
  1820. objsym.alignment:=needtowritealignmentalsoforELF;
  1821. end
  1822. else
  1823. {$endif USE_COMM_IN_BSS}
  1824. begin
  1825. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1826. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1827. objsym.size:=Tai_datablock(hp).size;
  1828. ObjData.alloc(Tai_datablock(hp).size);
  1829. end;
  1830. end;
  1831. ait_realconst:
  1832. ObjData.alloc(tai_realconst(hp).savesize);
  1833. ait_const:
  1834. begin
  1835. { Recalculate relative symbols }
  1836. if assigned(tai_const(hp).sym) and
  1837. assigned(tai_const(hp).endsym) then
  1838. begin
  1839. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1840. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1841. if Tai_const(hp).consttype in [aitconst_gottpoff,aitconst_tlsgd,aitconst_tlsdesc] then
  1842. begin
  1843. if objsymend.objsection<>ObjData.CurrObjSec then
  1844. Internalerror(2019092801);
  1845. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  1846. end
  1847. else if objsymend.objsection<>objsym.objsection then
  1848. begin
  1849. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1850. (objsym.objsection<>ObjData.CurrObjSec) then
  1851. internalerror(200905042);
  1852. end
  1853. {$push} {$R-}{$Q-}
  1854. else
  1855. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1856. end;
  1857. {$pop}
  1858. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1859. Tai_const(hp).fixsize;
  1860. ObjData.alloc(tai_const(hp).size);
  1861. end;
  1862. ait_section:
  1863. begin
  1864. { use cached value }
  1865. ObjData.setsection(Tai_section(hp).sec);
  1866. end;
  1867. ait_stab :
  1868. begin
  1869. if assigned(Tai_stab(hp).str) then
  1870. WriteStab(Tai_stab(hp).str);
  1871. end;
  1872. ait_symbol :
  1873. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1874. ait_symbol_end :
  1875. begin
  1876. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1877. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1878. end;
  1879. ait_symbolpair:
  1880. with tai_symbolpair(hp) do
  1881. ObjData.SymbolPairDefine(kind,sym^,value^);
  1882. ait_label :
  1883. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1884. ait_string :
  1885. ObjData.alloc(Tai_string(hp).len);
  1886. ait_instruction :
  1887. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1888. ait_cutobject :
  1889. if SmartAsm then
  1890. break;
  1891. ait_directive :
  1892. begin
  1893. case tai_directive(hp).directive of
  1894. asd_indirect_symbol:
  1895. if tai_directive(hp).name='' then
  1896. Internalerror(2009101103)
  1897. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1898. Internalerror(2009101102);
  1899. asd_lazy_reference:
  1900. { handled in TreePass0 }
  1901. ;
  1902. asd_reference:
  1903. { ignore for now, but should be added}
  1904. ;
  1905. asd_thumb_func:
  1906. { ignore for now, but should be added}
  1907. ;
  1908. asd_force_thumb:
  1909. { ignore for now, but should be added}
  1910. ;
  1911. asd_code:
  1912. { ignore for now, but should be added}
  1913. ;
  1914. asd_option:
  1915. { ignore for now, but should be added}
  1916. ;
  1917. asd_weak_definition:
  1918. { ignore for now, but should be added}
  1919. ;
  1920. {$ifdef OMFOBJSUPPORT}
  1921. asd_omf_linnum_line:
  1922. { ignore for now, but should be added}
  1923. ;
  1924. {$endif OMFOBJSUPPORT}
  1925. asd_cpu:
  1926. begin
  1927. ObjData.CPUType:=cpu_none;
  1928. for cpu:=low(tcputype) to high(tcputype) do
  1929. if cputypestr[cpu]=tai_directive(hp).name then
  1930. begin
  1931. ObjData.CPUType:=cpu;
  1932. break;
  1933. end;
  1934. end;
  1935. else
  1936. internalerror(2010011102);
  1937. end;
  1938. end;
  1939. ait_eabi_attribute :
  1940. begin
  1941. eabi_section:=ObjData.findsection('.ARM.attributes');
  1942. if not(assigned(eabi_section)) then
  1943. Internalerror(2019100702);
  1944. if eabi_section.Size=0 then
  1945. eabi_section.alloc(16);
  1946. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1947. case tai_eabi_attribute(hp).eattr_typ of
  1948. eattrtype_dword:
  1949. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1950. eattrtype_ntbs:
  1951. if assigned(tai_eabi_attribute(hp).valuestr) then
  1952. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1953. else
  1954. eabi_section.alloc(1);
  1955. else
  1956. Internalerror(2019100703);
  1957. end;
  1958. end;
  1959. else
  1960. ;
  1961. end;
  1962. hp:=Tai(hp.next);
  1963. end;
  1964. TreePass1:=hp;
  1965. end;
  1966. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1967. var
  1968. fillbuffer : tfillbuffer;
  1969. leblen : byte;
  1970. lebbuf : array[0..63] of byte;
  1971. objsym,
  1972. ref,
  1973. objsymend : TObjSymbol;
  1974. zerobuf : array[0..63] of byte;
  1975. relative_reloc: boolean;
  1976. pdata : pointer;
  1977. ssingle : single;
  1978. ddouble : double;
  1979. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1980. eextended : extended;
  1981. {$else}
  1982. {$ifdef FPC_SOFT_FPUX80}
  1983. f32 : float32;
  1984. f64 : float64;
  1985. eextended : floatx80;
  1986. {$endif}
  1987. {$endif}
  1988. ccomp : comp;
  1989. tmp : word;
  1990. cpu: tcputype;
  1991. ddword : dword;
  1992. eabi_section: TObjSection;
  1993. s: String;
  1994. TmpDataPos: TObjSectionOfs;
  1995. begin
  1996. fillchar(zerobuf,sizeof(zerobuf),0);
  1997. fillchar(objsym,sizeof(objsym),0);
  1998. fillchar(objsymend,sizeof(objsymend),0);
  1999. { main loop }
  2000. while assigned(hp) do
  2001. begin
  2002. {$ifdef DEBUG}
  2003. if not(hp.typ in SkipLineInfo) then
  2004. current_filepos:=tailineinfo(hp).fileinfo;
  2005. {$endif DEBUG}
  2006. case hp.typ of
  2007. ait_align :
  2008. begin
  2009. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  2010. InternalError(2012072301);
  2011. if oso_data in ObjData.CurrObjSec.secoptions then
  2012. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  2013. Tai_align_abstract(hp).fillsize)
  2014. else
  2015. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  2016. end;
  2017. ait_section :
  2018. begin
  2019. { use cached value }
  2020. ObjData.setsection(Tai_section(hp).sec);
  2021. end;
  2022. ait_symbol :
  2023. begin
  2024. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  2025. end;
  2026. ait_symbol_end :
  2027. begin
  2028. { recalculate size, as some preceding instructions
  2029. could have been changed to smaller size }
  2030. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  2031. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  2032. end;
  2033. ait_datablock :
  2034. begin
  2035. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  2036. {$ifdef USE_COMM_IN_BSS}
  2037. if not(writingpackages and
  2038. Tai_datablock(hp).is_global) then
  2039. {$endif USE_COMM_IN_BSS}
  2040. begin
  2041. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  2042. ObjData.alloc(Tai_datablock(hp).size);
  2043. end;
  2044. end;
  2045. ait_realconst:
  2046. begin
  2047. case tai_realconst(hp).realtyp of
  2048. aitrealconst_s32bit:
  2049. begin
  2050. ssingle:=single(tai_realconst(hp).value.s32val);
  2051. pdata:=@ssingle;
  2052. end;
  2053. aitrealconst_s64bit:
  2054. begin
  2055. ddouble:=double(tai_realconst(hp).value.s64val);
  2056. pdata:=@ddouble;
  2057. end;
  2058. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  2059. { can't write full 80 bit floating point constants yet on non-x86 }
  2060. aitrealconst_s80bit:
  2061. begin
  2062. eextended:=extended(tai_realconst(hp).value.s80val);
  2063. pdata:=@eextended;
  2064. end;
  2065. {$else}
  2066. {$ifdef FPC_SOFT_FPUX80}
  2067. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  2068. aitrealconst_s80bit:
  2069. begin
  2070. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  2071. begin
  2072. f64:=float64(double(tai_realconst(hp).value.s80val));
  2073. if float64_is_signaling_nan(f64)<>0 then
  2074. begin
  2075. f64.low := 0;
  2076. f64.high := longword($fff80000);
  2077. end;
  2078. eextended:=float64_to_floatx80(f64);
  2079. end
  2080. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  2081. begin
  2082. f32:=float32(single(tai_realconst(hp).value.s80val));
  2083. if float32_is_signaling_nan(f32)<>0 then
  2084. begin
  2085. f32 := longword($ffc00000);
  2086. end;
  2087. eextended:=float32_to_floatx80(f32);
  2088. end
  2089. else
  2090. internalerror(2017091903);
  2091. pdata:=@eextended;
  2092. end;
  2093. {$pop}
  2094. {$endif}
  2095. {$endif cpuextended}
  2096. aitrealconst_s64comp:
  2097. begin
  2098. ccomp:=comp(tai_realconst(hp).value.s64compval);
  2099. pdata:=@ccomp;
  2100. end;
  2101. else
  2102. internalerror(2015030501);
  2103. end;
  2104. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  2105. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  2106. end;
  2107. ait_string :
  2108. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  2109. ait_const :
  2110. begin
  2111. { Recalculate relative symbols, addresses of forward references
  2112. can be changed in treepass1 }
  2113. relative_reloc:=false;
  2114. if assigned(tai_const(hp).sym) and
  2115. assigned(tai_const(hp).endsym) then
  2116. begin
  2117. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  2118. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  2119. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  2120. if Tai_const(hp).consttype in [aitconst_gottpoff] then
  2121. begin
  2122. if objsymend.objsection<>ObjData.CurrObjSec then
  2123. Internalerror(2019092802);
  2124. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  2125. end
  2126. else if Tai_const(hp).consttype in [aitconst_tlsgd,aitconst_tlsdesc] then
  2127. begin
  2128. if objsymend.objsection<>ObjData.CurrObjSec then
  2129. Internalerror(2019092803);
  2130. Tai_const(hp).value:=ObjData.CurrObjSec.Size-objsymend.address+Tai_const(hp).symofs;
  2131. end
  2132. else if objsymend.objsection<>objsym.objsection then
  2133. begin
  2134. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  2135. (objsym.objsection<>ObjData.CurrObjSec) then
  2136. internalerror(2019010301);
  2137. end
  2138. else
  2139. {$push} {$R-}{$Q-}
  2140. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  2141. end;
  2142. {$pop}
  2143. case tai_const(hp).consttype of
  2144. aitconst_64bit,
  2145. aitconst_32bit,
  2146. aitconst_16bit,
  2147. aitconst_64bit_unaligned,
  2148. aitconst_32bit_unaligned,
  2149. aitconst_16bit_unaligned,
  2150. aitconst_8bit :
  2151. begin
  2152. if assigned(tai_const(hp).sym) and
  2153. not assigned(tai_const(hp).endsym) then
  2154. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  2155. else if relative_reloc then
  2156. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  2157. else
  2158. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2159. end;
  2160. aitconst_rva_symbol :
  2161. begin
  2162. { PE32+? }
  2163. if target_info.system in systems_peoptplus then
  2164. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  2165. else
  2166. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  2167. end;
  2168. aitconst_secrel32_symbol :
  2169. begin
  2170. { Required for DWARF2 support under Windows }
  2171. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  2172. end;
  2173. {$ifdef i8086}
  2174. aitconst_farptr :
  2175. if assigned(tai_const(hp).sym) and
  2176. not assigned(tai_const(hp).endsym) then
  2177. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  2178. else if relative_reloc then
  2179. internalerror(2015040601)
  2180. else
  2181. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2182. aitconst_seg:
  2183. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  2184. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  2185. else
  2186. internalerror(2015110502);
  2187. aitconst_dgroup:
  2188. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  2189. aitconst_fardataseg:
  2190. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  2191. {$endif i8086}
  2192. {$ifdef arm}
  2193. aitconst_got:
  2194. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  2195. { aitconst_gottpoff:
  2196. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF); }
  2197. aitconst_tpoff:
  2198. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF);
  2199. aitconst_tlsgd:
  2200. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSGD);
  2201. aitconst_tlsdesc:
  2202. begin
  2203. { must be a relative symbol, thus value being valid }
  2204. if not(assigned(tai_const(hp).sym)) or not(assigned(tai_const(hp).endsym)) then
  2205. Internalerror(2019092904);
  2206. ObjData.writereloc(Tai_const(hp).value,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSDESC);
  2207. end;
  2208. {$endif arm}
  2209. aitconst_dtpoff:
  2210. { so far, the size of dtpoff is fixed to 4 bytes }
  2211. ObjData.writereloc(Tai_const(hp).symofs,4,Objdata.SymbolRef(tai_const(hp).sym),RELOC_DTPOFF);
  2212. aitconst_gotoff_symbol:
  2213. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  2214. aitconst_uleb128bit,
  2215. aitconst_sleb128bit :
  2216. begin
  2217. if Tai_const(hp).fixed_size=0 then
  2218. Internalerror(2019030302);
  2219. if tai_const(hp).consttype=aitconst_uleb128bit then
  2220. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf,Tai_const(hp).fixed_size)
  2221. else
  2222. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf,Tai_const(hp).fixed_size);
  2223. if leblen<>tai_const(hp).fixed_size then
  2224. internalerror(200709271);
  2225. ObjData.writebytes(lebbuf,leblen);
  2226. end;
  2227. aitconst_darwin_dwarf_delta32,
  2228. aitconst_darwin_dwarf_delta64:
  2229. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2230. aitconst_half16bit,
  2231. aitconst_gs:
  2232. begin
  2233. tmp:=Tai_const(hp).value div 2;
  2234. ObjData.writebytes(tmp,2);
  2235. end;
  2236. else
  2237. internalerror(200603254);
  2238. end;
  2239. end;
  2240. ait_label :
  2241. begin
  2242. { exporting shouldn't be necessary as labels are local,
  2243. but it's better to be on the safe side (PFV) }
  2244. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  2245. end;
  2246. ait_instruction :
  2247. Taicpu(hp).Pass2(ObjData);
  2248. ait_stab :
  2249. WriteStab(Tai_stab(hp).str);
  2250. ait_function_name,
  2251. ait_force_line : ;
  2252. ait_cutobject :
  2253. if SmartAsm then
  2254. break;
  2255. ait_directive :
  2256. begin
  2257. case tai_directive(hp).directive of
  2258. asd_weak_definition,
  2259. asd_weak_reference:
  2260. begin
  2261. objsym:=ObjData.symbolref(tai_directive(hp).name);
  2262. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  2263. objsym.bind:=AB_WEAK_EXTERNAL
  2264. else
  2265. { TODO: should become a weak definition; for now, do
  2266. the same as what was done for ait_weak }
  2267. objsym.bind:=AB_WEAK_EXTERNAL;
  2268. end;
  2269. asd_cpu:
  2270. begin
  2271. ObjData.CPUType:=cpu_none;
  2272. for cpu:=low(tcputype) to high(tcputype) do
  2273. if cputypestr[cpu]=tai_directive(hp).name then
  2274. begin
  2275. ObjData.CPUType:=cpu;
  2276. break;
  2277. end;
  2278. end;
  2279. {$ifdef OMFOBJSUPPORT}
  2280. asd_omf_linnum_line:
  2281. begin
  2282. TOmfObjSection(ObjData.CurrObjSec).LinNumEntries.Add(
  2283. TOmfSubRecord_LINNUM_MsLink_Entry.Create(
  2284. strtoint(tai_directive(hp).name),
  2285. ObjData.CurrObjSec.Size
  2286. ));
  2287. end;
  2288. {$endif OMFOBJSUPPORT}
  2289. else
  2290. ;
  2291. end
  2292. end;
  2293. ait_symbolpair:
  2294. begin
  2295. if tai_symbolpair(hp).kind=spk_set then
  2296. begin
  2297. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  2298. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  2299. objsym.offset:=ref.offset;
  2300. objsym.objsection:=ref.objsection;
  2301. {$ifdef arm}
  2302. objsym.ThumbFunc:=ref.ThumbFunc;
  2303. {$endif arm}
  2304. end;
  2305. end;
  2306. {$ifndef DISABLE_WIN64_SEH}
  2307. ait_seh_directive :
  2308. tai_seh_directive(hp).generate_code(objdata);
  2309. {$endif DISABLE_WIN64_SEH}
  2310. ait_eabi_attribute :
  2311. begin
  2312. eabi_section:=ObjData.findsection('.ARM.attributes');
  2313. if not(assigned(eabi_section)) then
  2314. Internalerror(2019100704);
  2315. if eabi_section.Size=0 then
  2316. begin
  2317. s:='A';
  2318. eabi_section.write(s[1],1);
  2319. ddword:=eabi_section.Size-1;
  2320. eabi_section.write(ddword,4);
  2321. s:='aeabi'#0;
  2322. eabi_section.write(s[1],6);
  2323. s:=#1;
  2324. eabi_section.write(s[1],1);
  2325. ddword:=eabi_section.Size-1-4-6-1;
  2326. eabi_section.write(ddword,4);
  2327. end;
  2328. leblen:=EncodeUleb128(tai_eabi_attribute(hp).tag,lebbuf,0);
  2329. eabi_section.write(lebbuf,leblen);
  2330. case tai_eabi_attribute(hp).eattr_typ of
  2331. eattrtype_dword:
  2332. begin
  2333. leblen:=EncodeUleb128(tai_eabi_attribute(hp).value,lebbuf,0);
  2334. eabi_section.write(lebbuf,leblen);
  2335. end;
  2336. eattrtype_ntbs:
  2337. begin
  2338. if assigned(tai_eabi_attribute(hp).valuestr) then
  2339. s:=tai_eabi_attribute(hp).valuestr^+#0
  2340. else
  2341. s:=#0;
  2342. eabi_section.write(s[1],Length(s));
  2343. end
  2344. else
  2345. Internalerror(2019100705);
  2346. end;
  2347. { update size of attributes section, write directly to the dyn. arrays as
  2348. we do not increase the size of section }
  2349. TmpDataPos:=eabi_section.Data.Pos;
  2350. eabi_section.Data.seek(1);
  2351. ddword:=eabi_section.Size-1;
  2352. eabi_section.Data.write(ddword,4);
  2353. eabi_section.Data.seek(12);
  2354. ddword:=eabi_section.Size-1-4-6;
  2355. eabi_section.Data.write(ddword,4);
  2356. eabi_section.Data.Seek(TmpDataPos);
  2357. end;
  2358. else
  2359. ;
  2360. end;
  2361. hp:=Tai(hp.next);
  2362. end;
  2363. TreePass2:=hp;
  2364. end;
  2365. procedure TInternalAssembler.writetree;
  2366. label
  2367. doexit;
  2368. var
  2369. hp : Tai;
  2370. ObjWriter : TObjectWriter;
  2371. begin
  2372. ObjWriter:=TObjectwriter.create;
  2373. ObjOutput:=CObjOutput.Create(ObjWriter);
  2374. ObjData:=ObjOutput.newObjData(ObjFileName);
  2375. { Pass 0 }
  2376. ObjData.currpass:=0;
  2377. ObjData.createsection(sec_code);
  2378. ObjData.beforealloc;
  2379. { start with list 1 }
  2380. currlistidx:=1;
  2381. currlist:=list[currlistidx];
  2382. hp:=Tai(currList.first);
  2383. while assigned(hp) do
  2384. begin
  2385. hp:=TreePass0(hp);
  2386. MaybeNextList(hp);
  2387. end;
  2388. ObjData.afteralloc;
  2389. { leave if errors have occurred }
  2390. if errorcount>0 then
  2391. goto doexit;
  2392. { Pass 1 }
  2393. ObjData.currpass:=1;
  2394. ObjData.resetsections;
  2395. ObjData.beforealloc;
  2396. ObjData.createsection(sec_code);
  2397. { start with list 1 }
  2398. currlistidx:=1;
  2399. currlist:=list[currlistidx];
  2400. hp:=Tai(currList.first);
  2401. while assigned(hp) do
  2402. begin
  2403. hp:=TreePass1(hp);
  2404. MaybeNextList(hp);
  2405. end;
  2406. ObjData.createsection(sec_code);
  2407. ObjData.afteralloc;
  2408. { leave if errors have occurred }
  2409. if errorcount>0 then
  2410. goto doexit;
  2411. { Pass 2 }
  2412. ObjData.currpass:=2;
  2413. ObjData.resetsections;
  2414. ObjData.beforewrite;
  2415. ObjData.createsection(sec_code);
  2416. { start with list 1 }
  2417. currlistidx:=1;
  2418. currlist:=list[currlistidx];
  2419. hp:=Tai(currList.first);
  2420. while assigned(hp) do
  2421. begin
  2422. hp:=TreePass2(hp);
  2423. MaybeNextList(hp);
  2424. end;
  2425. ObjData.createsection(sec_code);
  2426. ObjData.afterwrite;
  2427. { don't write the .o file if errors have occurred }
  2428. if errorcount=0 then
  2429. begin
  2430. { write objectfile }
  2431. ObjOutput.startobjectfile(ObjFileName);
  2432. ObjOutput.writeobjectfile(ObjData);
  2433. end;
  2434. doexit:
  2435. { Cleanup }
  2436. ObjData.free;
  2437. ObjData:=nil;
  2438. ObjWriter.free;
  2439. end;
  2440. procedure TInternalAssembler.writetreesmart;
  2441. var
  2442. hp : Tai;
  2443. startsectype : TAsmSectiontype;
  2444. place: tcutplace;
  2445. ObjWriter : TObjectWriter;
  2446. startsecname: String;
  2447. startsecorder: TAsmSectionOrder;
  2448. begin
  2449. if not(cs_asm_leave in current_settings.globalswitches) and
  2450. not(af_needar in asminfo^.flags) then
  2451. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  2452. else
  2453. ObjWriter:=TObjectwriter.create;
  2454. NextSmartName(cut_normal);
  2455. ObjOutput:=CObjOutput.Create(ObjWriter);
  2456. startsectype:=sec_none;
  2457. startsecname:='';
  2458. startsecorder:=secorder_default;
  2459. { start with list 1 }
  2460. currlistidx:=1;
  2461. currlist:=list[currlistidx];
  2462. hp:=Tai(currList.first);
  2463. while assigned(hp) do
  2464. begin
  2465. ObjData:=ObjOutput.newObjData(ObjFileName);
  2466. { Pass 0 }
  2467. ObjData.currpass:=0;
  2468. ObjData.resetsections;
  2469. ObjData.beforealloc;
  2470. if startsectype<>sec_none then
  2471. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2472. TreePass0(hp);
  2473. ObjData.afteralloc;
  2474. { leave if errors have occurred }
  2475. if errorcount>0 then
  2476. break;
  2477. { Pass 1 }
  2478. ObjData.currpass:=1;
  2479. ObjData.resetsections;
  2480. ObjData.beforealloc;
  2481. if startsectype<>sec_none then
  2482. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2483. TreePass1(hp);
  2484. ObjData.afteralloc;
  2485. { leave if errors have occurred }
  2486. if errorcount>0 then
  2487. break;
  2488. { Pass 2 }
  2489. ObjData.currpass:=2;
  2490. ObjOutput.startobjectfile(ObjFileName);
  2491. ObjData.resetsections;
  2492. ObjData.beforewrite;
  2493. if startsectype<>sec_none then
  2494. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2495. hp:=TreePass2(hp);
  2496. ObjData.afterwrite;
  2497. { leave if errors have occurred }
  2498. if errorcount>0 then
  2499. break;
  2500. { write the current objectfile }
  2501. ObjOutput.writeobjectfile(ObjData);
  2502. ObjData.free;
  2503. ObjData:=nil;
  2504. { end of lists? }
  2505. if not MaybeNextList(hp) then
  2506. break;
  2507. { we will start a new objectfile so reset everything }
  2508. { The place can still change in the next while loop, so don't init }
  2509. { the writer yet (JM) }
  2510. if (hp.typ=ait_cutobject) then
  2511. place := Tai_cutobject(hp).place
  2512. else
  2513. place := cut_normal;
  2514. { avoid empty files }
  2515. startsectype:=sec_none;
  2516. startsecname:='';
  2517. startsecorder:=secorder_default;
  2518. while assigned(hp) and
  2519. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2520. begin
  2521. if Tai(hp).typ=ait_section then
  2522. begin
  2523. startsectype:=Tai_section(hp).sectype;
  2524. startsecname:=Tai_section(hp).name^;
  2525. startsecorder:=Tai_section(hp).secorder;
  2526. end;
  2527. if (Tai(hp).typ=ait_cutobject) then
  2528. place:=Tai_cutobject(hp).place;
  2529. hp:=Tai(hp.next);
  2530. end;
  2531. if not MaybeNextList(hp) then
  2532. break;
  2533. { start next objectfile }
  2534. NextSmartName(place);
  2535. end;
  2536. ObjData.free;
  2537. ObjData:=nil;
  2538. ObjWriter.free;
  2539. end;
  2540. procedure TInternalAssembler.MakeObject;
  2541. var to_do:set of TasmlistType;
  2542. i:TasmlistType;
  2543. procedure addlist(p:TAsmList);
  2544. begin
  2545. inc(lists);
  2546. list[lists]:=p;
  2547. end;
  2548. begin
  2549. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2550. if usedeffileforexports then
  2551. exclude(to_do,al_exports);
  2552. if not(tf_section_threadvars in target_info.flags) then
  2553. exclude(to_do,al_threadvars);
  2554. for i:=low(TasmlistType) to high(TasmlistType) do
  2555. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2556. (not current_asmdata.asmlists[i].empty) then
  2557. addlist(current_asmdata.asmlists[i]);
  2558. if SmartAsm then
  2559. writetreesmart
  2560. else
  2561. writetree;
  2562. end;
  2563. {*****************************************************************************
  2564. Generate Assembler Files Main Procedure
  2565. *****************************************************************************}
  2566. Procedure GenerateAsm(smart:boolean);
  2567. var
  2568. a : TAssembler;
  2569. begin
  2570. if not assigned(CAssembler[target_asm.id]) then
  2571. Message(asmw_f_assembler_output_not_supported);
  2572. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2573. a.MakeObject;
  2574. a.Free;
  2575. end;
  2576. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2577. var
  2578. asmkind: tasm;
  2579. begin
  2580. for asmkind in [as_gas,as_ggas,as_darwin,as_clang_gas,as_clang_asdarwin] do
  2581. if assigned(asminfos[asmkind]) and
  2582. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2583. begin
  2584. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2585. exit;
  2586. end;
  2587. Internalerror(2015090604);
  2588. end;
  2589. {*****************************************************************************
  2590. Init/Done
  2591. *****************************************************************************}
  2592. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2593. var
  2594. t : tasm;
  2595. begin
  2596. t:=r.id;
  2597. if assigned(asminfos[t]) then
  2598. writeln('Warning: Assembler is already registered!')
  2599. else
  2600. Getmem(asminfos[t],sizeof(tasminfo));
  2601. asminfos[t]^:=r;
  2602. CAssembler[t]:=c;
  2603. end;
  2604. end.