assemble.pas 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781
  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. ccomp: comp;
  982. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  983. eextended: extended;
  984. {$else}
  985. {$ifdef FPC_SOFT_FPUX80}
  986. f32 : float32;
  987. f64 : float64;
  988. eextended: floatx80;
  989. {$endif}
  990. {$endif cpuextended}
  991. begin
  992. if do_line then
  993. begin
  994. case tai_realconst(hp).realtyp of
  995. aitrealconst_s32bit:
  996. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  997. aitrealconst_s64bit:
  998. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  999. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1000. { can't write full 80 bit floating point constants yet on non-x86 }
  1001. aitrealconst_s80bit:
  1002. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  1003. {$else}
  1004. {$ifdef FPC_SOFT_FPUX80}
  1005. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1006. aitrealconst_s80bit:
  1007. begin
  1008. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1009. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  1010. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1011. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  1012. else
  1013. internalerror(2017091901);
  1014. end;
  1015. {$pop}
  1016. {$endif}
  1017. {$endif cpuextended}
  1018. aitrealconst_s64comp:
  1019. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  1020. else
  1021. internalerror(2014050604);
  1022. end;
  1023. end;
  1024. writer.AsmWrite(dbdir);
  1025. { generic float writing code: get start address of value, then write
  1026. byte by byte. Can't use fields directly, because e.g ts64comp is
  1027. defined as extended on x86 }
  1028. case tai_realconst(hp).realtyp of
  1029. aitrealconst_s32bit:
  1030. begin
  1031. ssingle:=single(tai_realconst(hp).value.s32val);
  1032. pdata:=@ssingle;
  1033. end;
  1034. aitrealconst_s64bit:
  1035. begin
  1036. ddouble:=double(tai_realconst(hp).value.s64val);
  1037. pdata:=@ddouble;
  1038. end;
  1039. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1040. { can't write full 80 bit floating point constants yet on non-x86 }
  1041. aitrealconst_s80bit:
  1042. begin
  1043. eextended:=extended(tai_realconst(hp).value.s80val);
  1044. pdata:=@eextended;
  1045. end;
  1046. {$else}
  1047. {$ifdef FPC_SOFT_FPUX80}
  1048. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  1049. aitrealconst_s80bit:
  1050. begin
  1051. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  1052. begin
  1053. f64:=float64(double(tai_realconst(hp).value.s80val));
  1054. if float64_is_signaling_nan(f64)<>0 then
  1055. begin
  1056. f64.low := 0;
  1057. f64.high := longword($fff80000);
  1058. end;
  1059. eextended:=float64_to_floatx80(f64);
  1060. end
  1061. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  1062. begin
  1063. f32:=float32(single(tai_realconst(hp).value.s80val));
  1064. if float32_is_signaling_nan(f32)<>0 then
  1065. begin
  1066. f32 := longword($ffc00000);
  1067. end;
  1068. eextended:=float32_to_floatx80(f32);
  1069. end
  1070. else
  1071. internalerror(2017091902);
  1072. pdata:=@eextended;
  1073. end;
  1074. {$pop}
  1075. {$endif}
  1076. {$endif cpuextended}
  1077. aitrealconst_s64comp:
  1078. begin
  1079. ccomp:=comp(tai_realconst(hp).value.s64compval);
  1080. pdata:=@ccomp;
  1081. end;
  1082. else
  1083. internalerror(2014051001);
  1084. end;
  1085. count:=tai_realconst(hp).datasize;
  1086. { write bytes in inverse order if source and target endianess don't
  1087. match }
  1088. if source_info.endian<>target_info.endian then
  1089. begin
  1090. { go from back to front }
  1091. index:=count-1;
  1092. step:=-1;
  1093. end
  1094. else
  1095. begin
  1096. index:=0;
  1097. step:=1;
  1098. end;
  1099. {$ifdef ARM}
  1100. { ARM-specific: low and high dwords of a double may be swapped }
  1101. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  1102. begin
  1103. { only supported for double }
  1104. if tai_realconst(hp).datasize<>8 then
  1105. internalerror(2014050605);
  1106. { switch bit of the index so that the words are written in
  1107. the opposite order }
  1108. swapmask:=4;
  1109. end
  1110. else
  1111. {$endif ARM}
  1112. swapmask:=0;
  1113. repeat
  1114. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  1115. inc(index,step);
  1116. dec(count);
  1117. if count<>0 then
  1118. writer.AsmWrite(',');
  1119. until count=0;
  1120. { padding }
  1121. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  1122. writer.AsmWrite(',0');
  1123. writer.AsmLn;
  1124. end;
  1125. function TExternalAssembler.WriteComments(var hp: tai): boolean;
  1126. begin
  1127. result:=true;
  1128. case hp.typ of
  1129. ait_comment :
  1130. Begin
  1131. writer.AsmWrite(asminfo^.comment);
  1132. writer.AsmWritePChar(tai_comment(hp).str);
  1133. writer.AsmLn;
  1134. End;
  1135. ait_regalloc :
  1136. begin
  1137. if (cs_asm_regalloc in current_settings.globalswitches) then
  1138. begin
  1139. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  1140. repeat
  1141. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  1142. if (hp.next=nil) or
  1143. (tai(hp.next).typ<>ait_regalloc) or
  1144. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  1145. break;
  1146. hp:=tai(hp.next);
  1147. writer.AsmWrite(',');
  1148. until false;
  1149. writer.AsmWrite(' ');
  1150. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  1151. end;
  1152. end;
  1153. ait_tempalloc :
  1154. begin
  1155. if (cs_asm_tempalloc in current_settings.globalswitches) then
  1156. WriteTempalloc(tai_tempalloc(hp));
  1157. end;
  1158. ait_varloc:
  1159. begin
  1160. { ait_varloc is present here only when register allocation is not done ( -sr option ) }
  1161. if tai_varloc(hp).newlocationhi<>NR_NO then
  1162. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1163. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1164. else
  1165. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1166. std_regname(tai_varloc(hp).newlocation));
  1167. end;
  1168. else
  1169. result:=false;
  1170. end;
  1171. end;
  1172. procedure TExternalAssembler.WriteTree(p:TAsmList);
  1173. begin
  1174. end;
  1175. procedure TExternalAssembler.WriteAsmList;
  1176. begin
  1177. end;
  1178. procedure TExternalAssembler.MakeObject;
  1179. begin
  1180. writer.AsmCreate(cut_normal);
  1181. FillChar(lastfileinfo, sizeof(lastfileinfo), 0);
  1182. lastfileinfo.line := -1;
  1183. lastinfile := nil;
  1184. lastsectype := sec_none;
  1185. WriteAsmList;
  1186. writer.AsmClose;
  1187. if not(writer.ioerror) then
  1188. DoAssemble;
  1189. end;
  1190. destructor TExternalAssembler.Destroy;
  1191. begin
  1192. if ffreewriter then
  1193. writer.Free;
  1194. inherited;
  1195. end;
  1196. {*****************************************************************************
  1197. TInternalAssembler
  1198. *****************************************************************************}
  1199. constructor TInternalAssembler.Create(info: pasminfo; smart: boolean);
  1200. begin
  1201. inherited;
  1202. ObjOutput:=nil;
  1203. ObjData:=nil;
  1204. SmartAsm:=smart;
  1205. {$ifdef ARM}
  1206. Code16:=current_settings.instructionset=is_thumb;
  1207. {$endif ARM}
  1208. end;
  1209. destructor TInternalAssembler.destroy;
  1210. begin
  1211. if assigned(ObjData) then
  1212. ObjData.free;
  1213. if assigned(ObjOutput) then
  1214. ObjOutput.free;
  1215. end;
  1216. procedure TInternalAssembler.WriteStab(p:pchar);
  1217. function consumecomma(var p:pchar):boolean;
  1218. begin
  1219. while (p^=' ') do
  1220. inc(p);
  1221. result:=(p^=',');
  1222. inc(p);
  1223. end;
  1224. function consumenumber(var p:pchar;out value:longint):boolean;
  1225. var
  1226. hs : string;
  1227. len,
  1228. code : integer;
  1229. begin
  1230. value:=0;
  1231. while (p^=' ') do
  1232. inc(p);
  1233. len:=0;
  1234. while (p^ in ['0'..'9']) do
  1235. begin
  1236. inc(len);
  1237. hs[len]:=p^;
  1238. inc(p);
  1239. end;
  1240. if len>0 then
  1241. begin
  1242. hs[0]:=chr(len);
  1243. val(hs,value,code);
  1244. end
  1245. else
  1246. code:=-1;
  1247. result:=(code=0);
  1248. end;
  1249. function consumeoffset(var p:pchar;out relocsym:tobjsymbol;out value:longint):boolean;
  1250. var
  1251. hs : string;
  1252. len,
  1253. code : integer;
  1254. pstart : pchar;
  1255. sym : tobjsymbol;
  1256. exprvalue : longint;
  1257. gotmin,
  1258. have_first_symbol,
  1259. have_second_symbol,
  1260. dosub : boolean;
  1261. begin
  1262. result:=false;
  1263. value:=0;
  1264. relocsym:=nil;
  1265. gotmin:=false;
  1266. have_first_symbol:=false;
  1267. have_second_symbol:=false;
  1268. repeat
  1269. dosub:=false;
  1270. exprvalue:=0;
  1271. if gotmin then
  1272. begin
  1273. dosub:=true;
  1274. gotmin:=false;
  1275. end;
  1276. while (p^=' ') do
  1277. inc(p);
  1278. case p^ of
  1279. #0 :
  1280. break;
  1281. ' ' :
  1282. inc(p);
  1283. '0'..'9' :
  1284. begin
  1285. len:=0;
  1286. while (p^ in ['0'..'9']) do
  1287. begin
  1288. inc(len);
  1289. hs[len]:=p^;
  1290. inc(p);
  1291. end;
  1292. hs[0]:=chr(len);
  1293. val(hs,exprvalue,code);
  1294. if code<>0 then
  1295. internalerror(200702251);
  1296. end;
  1297. '.','_',
  1298. 'A'..'Z',
  1299. 'a'..'z' :
  1300. begin
  1301. pstart:=p;
  1302. while not(p^ in [#0,' ','-','+']) do
  1303. inc(p);
  1304. len:=p-pstart;
  1305. if len>255 then
  1306. internalerror(200509187);
  1307. hs[0]:=chr(len);
  1308. move(pstart^,hs[1],len);
  1309. sym:=objdata.symbolref(hs);
  1310. { Second symbol? }
  1311. if assigned(relocsym) then
  1312. begin
  1313. if have_second_symbol then
  1314. internalerror(2007032201);
  1315. have_second_symbol:=true;
  1316. if not have_first_symbol then
  1317. internalerror(2007032202);
  1318. { second symbol should substracted to first }
  1319. if not dosub then
  1320. internalerror(2007032203);
  1321. if (relocsym.objsection<>sym.objsection) then
  1322. internalerror(2005091810);
  1323. exprvalue:=relocsym.address-sym.address;
  1324. relocsym:=nil;
  1325. dosub:=false;
  1326. end
  1327. else
  1328. begin
  1329. relocsym:=sym;
  1330. if assigned(sym.objsection) then
  1331. begin
  1332. { first symbol should be + }
  1333. if not have_first_symbol and dosub then
  1334. internalerror(2007032204);
  1335. have_first_symbol:=true;
  1336. end;
  1337. end;
  1338. end;
  1339. '+' :
  1340. begin
  1341. { nothing, by default addition is done }
  1342. inc(p);
  1343. end;
  1344. '-' :
  1345. begin
  1346. gotmin:=true;
  1347. inc(p);
  1348. end;
  1349. else
  1350. internalerror(200509189);
  1351. end;
  1352. if dosub then
  1353. dec(value,exprvalue)
  1354. else
  1355. inc(value,exprvalue);
  1356. until false;
  1357. result:=true;
  1358. end;
  1359. var
  1360. stabstrlen,
  1361. ofs,
  1362. nline,
  1363. nidx,
  1364. nother,
  1365. i : longint;
  1366. stab : TObjStabEntry;
  1367. relocsym : TObjSymbol;
  1368. pstr,
  1369. pcurr,
  1370. pendquote : pchar;
  1371. oldsec : TObjSection;
  1372. begin
  1373. pcurr:=nil;
  1374. pstr:=nil;
  1375. pendquote:=nil;
  1376. relocsym:=nil;
  1377. ofs:=0;
  1378. { Parse string part }
  1379. if (p[0]='"') then
  1380. begin
  1381. pstr:=@p[1];
  1382. { Ignore \" inside the string }
  1383. i:=1;
  1384. while not((p[i]='"') and (p[i-1]<>'\')) and
  1385. (p[i]<>#0) do
  1386. inc(i);
  1387. pendquote:=@p[i];
  1388. pendquote^:=#0;
  1389. pcurr:=@p[i+1];
  1390. if not consumecomma(pcurr) then
  1391. internalerror(200509181);
  1392. end
  1393. else
  1394. pcurr:=p;
  1395. { When in pass 1 then only alloc and leave }
  1396. if ObjData.currpass=1 then
  1397. begin
  1398. ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
  1399. if assigned(pstr) and (pstr[0]<>#0) then
  1400. ObjData.StabStrSec.Alloc(strlen(pstr)+1);
  1401. end
  1402. else
  1403. begin
  1404. { Stabs format: nidx,nother,nline[,offset] }
  1405. if not consumenumber(pcurr,nidx) then
  1406. internalerror(200509182);
  1407. if not consumecomma(pcurr) then
  1408. internalerror(200509183);
  1409. if not consumenumber(pcurr,nother) then
  1410. internalerror(200509184);
  1411. if not consumecomma(pcurr) then
  1412. internalerror(200509185);
  1413. if not consumenumber(pcurr,nline) then
  1414. internalerror(200509186);
  1415. if consumecomma(pcurr) then
  1416. consumeoffset(pcurr,relocsym,ofs);
  1417. { Generate stab entry }
  1418. if assigned(pstr) and (pstr[0]<>#0) then
  1419. begin
  1420. stabstrlen:=strlen(pstr);
  1421. {$ifdef optimizestabs}
  1422. StabStrEntry:=nil;
  1423. if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
  1424. begin
  1425. hs:=strpas(pstr);
  1426. StabstrEntry:=StabStrDict.Find(hs);
  1427. if not assigned(StabstrEntry) then
  1428. begin
  1429. StabstrEntry:=TStabStrEntry.Create(hs);
  1430. StabstrEntry:=StabStrSec.Size;
  1431. StabStrDict.Insert(StabstrEntry);
  1432. { generate new stab }
  1433. StabstrEntry:=nil;
  1434. end;
  1435. end;
  1436. if assigned(StabstrEntry) then
  1437. stab.strpos:=StabstrEntry.strpos
  1438. else
  1439. {$endif optimizestabs}
  1440. begin
  1441. stab.strpos:=ObjData.StabStrSec.Size;
  1442. ObjData.StabStrSec.write(pstr^,stabstrlen+1);
  1443. end;
  1444. end
  1445. else
  1446. stab.strpos:=0;
  1447. stab.ntype:=byte(nidx);
  1448. stab.ndesc:=word(nline);
  1449. stab.nother:=byte(nother);
  1450. stab.nvalue:=ofs;
  1451. { Write the stab first without the value field. Then
  1452. write a the value field with relocation }
  1453. oldsec:=ObjData.CurrObjSec;
  1454. ObjData.SetSection(ObjData.StabsSec);
  1455. ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
  1456. ObjData.Writereloc(stab.nvalue,4,relocsym,RELOC_ABSOLUTE32);
  1457. ObjData.setsection(oldsec);
  1458. end;
  1459. if assigned(pendquote) then
  1460. pendquote^:='"';
  1461. end;
  1462. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  1463. begin
  1464. { maybe end of list }
  1465. while not assigned(hp) do
  1466. begin
  1467. if currlistidx<lists then
  1468. begin
  1469. inc(currlistidx);
  1470. currlist:=list[currlistidx];
  1471. hp:=Tai(currList.first);
  1472. end
  1473. else
  1474. begin
  1475. MaybeNextList:=false;
  1476. exit;
  1477. end;
  1478. end;
  1479. MaybeNextList:=true;
  1480. end;
  1481. function TInternalAssembler.SetIndirectToSymbol(hp: Tai; const indirectname: string): Boolean;
  1482. var
  1483. objsym : TObjSymbol;
  1484. indsym : TObjSymbol;
  1485. begin
  1486. Result:=
  1487. Assigned(hp) and
  1488. (hp.typ=ait_symbol);
  1489. if not Result then
  1490. Exit;
  1491. objsym:=Objdata.SymbolRef(tai_symbol(hp).sym);
  1492. objsym.size:=0;
  1493. indsym := TObjSymbol(ObjData.ObjSymbolList.Find(indirectname));
  1494. if not Assigned(indsym) then
  1495. begin
  1496. { it's possible that indirect symbol is not present in the list,
  1497. so we must create it as undefined }
  1498. indsym:=ObjData.CObjSymbol.Create(ObjData.ObjSymbolList, indirectname);
  1499. indsym.typ:=AT_NONE;
  1500. indsym.bind:=AB_NONE;
  1501. end;
  1502. objsym.indsymbol:=indsym;
  1503. Result:=true;
  1504. end;
  1505. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  1506. var
  1507. objsym,
  1508. objsymend : TObjSymbol;
  1509. cpu: tcputype;
  1510. eabi_section, TmpSection: TObjSection;
  1511. begin
  1512. while assigned(hp) do
  1513. begin
  1514. {$ifdef DEBUG}
  1515. if not(hp.typ in SkipLineInfo) then
  1516. current_filepos:=tailineinfo(hp).fileinfo;
  1517. {$endif DEBUG}
  1518. case hp.typ of
  1519. ait_align :
  1520. begin
  1521. if tai_align_abstract(hp).aligntype>1 then
  1522. begin
  1523. { always use the maximum fillsize in this pass to avoid possible
  1524. short jumps to become out of range }
  1525. Tai_align_abstract(hp).fillsize:=Tai_align_abstract(hp).aligntype;
  1526. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1527. { may need to increase alignment of section }
  1528. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1529. ObjData.CurrObjSec.secalign:=tai_align_abstract(hp).aligntype;
  1530. end
  1531. else
  1532. Tai_align_abstract(hp).fillsize:=0;
  1533. end;
  1534. ait_datablock :
  1535. begin
  1536. {$ifdef USE_COMM_IN_BSS}
  1537. if writingpackages and
  1538. Tai_datablock(hp).is_global then
  1539. ObjData.SymbolDefine(Tai_datablock(hp).sym)
  1540. else
  1541. {$endif USE_COMM_IN_BSS}
  1542. begin
  1543. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1544. ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1545. ObjData.alloc(Tai_datablock(hp).size);
  1546. end;
  1547. end;
  1548. ait_realconst:
  1549. ObjData.alloc(tai_realconst(hp).savesize);
  1550. ait_const:
  1551. begin
  1552. { if symbols are provided we can calculate the value for relative symbols.
  1553. This is required for length calculation of leb128 constants }
  1554. if assigned(tai_const(hp).sym) then
  1555. begin
  1556. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1557. { objsym already defined and there is endsym? }
  1558. if assigned(objsym.objsection) and assigned(tai_const(hp).endsym) then
  1559. begin
  1560. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1561. { objsymend already defined? }
  1562. if assigned(objsymend.objsection) then
  1563. begin
  1564. if objsymend.objsection<>objsym.objsection then
  1565. begin
  1566. { leb128 relative constants are not relocatable, but other types are,
  1567. given that objsym belongs to the current section. }
  1568. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1569. (objsym.objsection<>ObjData.CurrObjSec) then
  1570. InternalError(200404124);
  1571. end
  1572. {$push} {$R-}{$Q-}
  1573. else
  1574. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1575. end;
  1576. {$pop}
  1577. end;
  1578. end;
  1579. ObjData.alloc(tai_const(hp).size);
  1580. end;
  1581. ait_directive:
  1582. begin
  1583. case tai_directive(hp).directive of
  1584. asd_indirect_symbol:
  1585. { handled in TreePass1 }
  1586. ;
  1587. asd_lazy_reference:
  1588. begin
  1589. if tai_directive(hp).name='' then
  1590. Internalerror(2009112101);
  1591. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1592. objsym.bind:=AB_LAZY;
  1593. end;
  1594. asd_reference:
  1595. { ignore for now, but should be added}
  1596. ;
  1597. asd_cpu:
  1598. begin
  1599. ObjData.CPUType:=cpu_none;
  1600. for cpu:=low(tcputype) to high(tcputype) do
  1601. if cputypestr[cpu]=tai_directive(hp).name then
  1602. begin
  1603. ObjData.CPUType:=cpu;
  1604. break;
  1605. end;
  1606. end;
  1607. asd_weak_definition:
  1608. begin
  1609. if tai_directive(hp).name='' then
  1610. Internalerror(2022040901);
  1611. objsym:=ObjData.symbolref(tai_directive(hp).name);
  1612. objsym.bind:=AB_WEAK;
  1613. end;
  1614. {$ifdef OMFOBJSUPPORT}
  1615. asd_omf_linnum_line:
  1616. { ignore for now, but should be added}
  1617. ;
  1618. {$endif OMFOBJSUPPORT}
  1619. {$ifdef ARM}
  1620. asd_thumb_func:
  1621. ObjData.ThumbFunc:=true;
  1622. asd_force_thumb:
  1623. begin
  1624. ObjData.ThumbFunc:=true;
  1625. Code16:=true;
  1626. end;
  1627. asd_code:
  1628. begin
  1629. { ai_directive(hp).name can be only 16 or 32, this is checked by the reader }
  1630. ObjData.ThumbFunc:=tai_directive(hp).name='16';
  1631. Code16:=tai_directive(hp).name='16';
  1632. end
  1633. {$endif ARM}
  1634. {$ifdef RISCV}
  1635. asd_option:
  1636. internalerror(2019031701);
  1637. {$endif RISCV}
  1638. else
  1639. internalerror(2010011101);
  1640. end;
  1641. end;
  1642. ait_section:
  1643. begin
  1644. if Tai_section(hp).sectype=sec_user then
  1645. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).secflags,Tai_section(hp).secprogbits,Tai_section(hp).name^,Tai_section(hp).secorder)
  1646. else
  1647. ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
  1648. Tai_section(hp).sec:=ObjData.CurrObjSec;
  1649. end;
  1650. ait_symbol :
  1651. begin
  1652. { needs extra support in the internal assembler }
  1653. { the value is just ignored }
  1654. {if tai_symbol(hp).has_value then
  1655. internalerror(2009090804); ;}
  1656. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1657. end;
  1658. ait_symbolpair :
  1659. with tai_symbolpair(hp) do
  1660. ObjData.SymbolPairDefine(kind,sym^,value^);
  1661. ait_label :
  1662. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1663. ait_string :
  1664. ObjData.alloc(Tai_string(hp).len);
  1665. ait_instruction :
  1666. begin
  1667. {$ifdef arm}
  1668. if code16 then
  1669. include(taicpu(hp).flags,cf_thumb)
  1670. else
  1671. exclude(taicpu(hp).flags,cf_thumb);
  1672. {$endif arm}
  1673. { reset instructions which could change in pass 2 }
  1674. Taicpu(hp).resetpass2;
  1675. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1676. end;
  1677. ait_cutobject :
  1678. if SmartAsm then
  1679. break;
  1680. ait_eabi_attribute :
  1681. begin
  1682. eabi_section:=ObjData.findsection('.ARM.attributes');
  1683. if not(assigned(eabi_section)) then
  1684. begin
  1685. TmpSection:=ObjData.CurrObjSec;
  1686. ObjData.CreateSection(sec_arm_attribute,[],SPB_ARM_ATTRIBUTES,'',secorder_default);
  1687. eabi_section:=ObjData.CurrObjSec;
  1688. ObjData.setsection(TmpSection);
  1689. end;
  1690. if eabi_section.Size=0 then
  1691. eabi_section.alloc(16);
  1692. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1693. case tai_eabi_attribute(hp).eattr_typ of
  1694. eattrtype_dword:
  1695. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1696. eattrtype_ntbs:
  1697. if assigned(tai_eabi_attribute(hp).valuestr) then
  1698. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1699. else
  1700. eabi_section.alloc(1);
  1701. else
  1702. Internalerror(2019100701);
  1703. end;
  1704. end;
  1705. {$ifdef WASM}
  1706. ait_globaltype:
  1707. TWasmObjData(ObjData).DeclareGlobalType(tai_globaltype(hp));
  1708. ait_functype:
  1709. TWasmObjData(ObjData).DeclareFuncType(tai_functype(hp));
  1710. ait_tagtype:
  1711. TWasmObjData(ObjData).DeclareTagType(tai_tagtype(hp));
  1712. ait_export_name:
  1713. TWasmObjData(ObjData).DeclareExportName(tai_export_name(hp));
  1714. ait_import_module:
  1715. TWasmObjData(ObjData).DeclareImportModule(tai_import_module(hp));
  1716. ait_import_name:
  1717. TWasmObjData(ObjData).DeclareImportName(tai_import_name(hp));
  1718. ait_local:
  1719. TWasmObjData(ObjData).DeclareLocal(tai_local(hp));
  1720. {$endif WASM}
  1721. else
  1722. ;
  1723. end;
  1724. hp:=Tai(hp.next);
  1725. end;
  1726. TreePass0:=hp;
  1727. end;
  1728. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  1729. var
  1730. objsym,
  1731. objsymend : TObjSymbol;
  1732. cpu: tcputype;
  1733. eabi_section: TObjSection;
  1734. begin
  1735. while assigned(hp) do
  1736. begin
  1737. {$ifdef DEBUG}
  1738. if not(hp.typ in SkipLineInfo) then
  1739. current_filepos:=tailineinfo(hp).fileinfo;
  1740. {$endif DEBUG}
  1741. case hp.typ of
  1742. ait_align :
  1743. begin
  1744. if tai_align_abstract(hp).aligntype>1 then
  1745. begin
  1746. { here we must determine the fillsize which is used in pass2 }
  1747. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Tai_align_abstract(hp).aligntype)-
  1748. ObjData.CurrObjSec.Size;
  1749. { maximum number of bytes for alignment exeeded? }
  1750. if (Tai_align_abstract(hp).aligntype<>Tai_align_abstract(hp).maxbytes) and
  1751. (Tai_align_abstract(hp).fillsize>Tai_align_abstract(hp).maxbytes) then
  1752. Tai_align_abstract(hp).fillsize:=align(ObjData.CurrObjSec.Size,Byte(Tai_align_abstract(hp).aligntype div 2))-
  1753. ObjData.CurrObjSec.Size;
  1754. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1755. end;
  1756. end;
  1757. ait_datablock :
  1758. begin
  1759. if (oso_data in ObjData.CurrObjSec.secoptions) and
  1760. not (oso_sparse_data in ObjData.CurrObjSec.secoptions) then
  1761. Message(asmw_e_alloc_data_only_in_bss);
  1762. {$ifdef USE_COMM_IN_BSS}
  1763. if writingpackages and
  1764. Tai_datablock(hp).is_global then
  1765. begin
  1766. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1767. objsym.size:=Tai_datablock(hp).size;
  1768. objsym.bind:=AB_COMMON;
  1769. objsym.alignment:=needtowritealignmentalsoforELF;
  1770. end
  1771. else
  1772. {$endif USE_COMM_IN_BSS}
  1773. begin
  1774. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1775. objsym:=ObjData.SymbolDefine(Tai_datablock(hp).sym);
  1776. objsym.size:=Tai_datablock(hp).size;
  1777. ObjData.alloc(Tai_datablock(hp).size);
  1778. end;
  1779. end;
  1780. ait_realconst:
  1781. ObjData.alloc(tai_realconst(hp).savesize);
  1782. ait_const:
  1783. begin
  1784. { Recalculate relative symbols }
  1785. if assigned(tai_const(hp).sym) and
  1786. assigned(tai_const(hp).endsym) then
  1787. begin
  1788. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  1789. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  1790. if Tai_const(hp).consttype in [aitconst_gottpoff,aitconst_tlsgd,aitconst_tlsdesc] then
  1791. begin
  1792. if objsymend.objsection<>ObjData.CurrObjSec then
  1793. Internalerror(2019092801);
  1794. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  1795. end
  1796. else if objsymend.objsection<>objsym.objsection then
  1797. begin
  1798. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  1799. (objsym.objsection<>ObjData.CurrObjSec) then
  1800. internalerror(200905042);
  1801. end
  1802. {$push} {$R-}{$Q-}
  1803. else
  1804. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  1805. end;
  1806. {$pop}
  1807. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1808. Tai_const(hp).fixsize;
  1809. ObjData.alloc(tai_const(hp).size);
  1810. end;
  1811. ait_section:
  1812. begin
  1813. { use cached value }
  1814. ObjData.setsection(Tai_section(hp).sec);
  1815. end;
  1816. ait_stab :
  1817. begin
  1818. if assigned(Tai_stab(hp).str) then
  1819. WriteStab(Tai_stab(hp).str);
  1820. end;
  1821. ait_symbol :
  1822. ObjData.SymbolDefine(Tai_symbol(hp).sym);
  1823. ait_symbol_end :
  1824. begin
  1825. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1826. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1827. end;
  1828. ait_symbolpair:
  1829. with tai_symbolpair(hp) do
  1830. ObjData.SymbolPairDefine(kind,sym^,value^);
  1831. ait_label :
  1832. ObjData.SymbolDefine(Tai_label(hp).labsym);
  1833. ait_string :
  1834. ObjData.alloc(Tai_string(hp).len);
  1835. ait_instruction :
  1836. ObjData.alloc(Taicpu(hp).Pass1(ObjData));
  1837. ait_cutobject :
  1838. if SmartAsm then
  1839. break;
  1840. ait_directive :
  1841. begin
  1842. case tai_directive(hp).directive of
  1843. asd_indirect_symbol:
  1844. if tai_directive(hp).name='' then
  1845. Internalerror(2009101103)
  1846. else if not SetIndirectToSymbol(Tai(hp.Previous), tai_directive(hp).name) then
  1847. Internalerror(2009101102);
  1848. asd_lazy_reference:
  1849. { handled in TreePass0 }
  1850. ;
  1851. asd_reference:
  1852. { ignore for now, but should be added}
  1853. ;
  1854. asd_thumb_func:
  1855. { ignore for now, but should be added}
  1856. ;
  1857. asd_force_thumb:
  1858. { ignore for now, but should be added}
  1859. ;
  1860. asd_code:
  1861. { ignore for now, but should be added}
  1862. ;
  1863. asd_option:
  1864. { ignore for now, but should be added}
  1865. ;
  1866. asd_weak_definition:
  1867. { ignore for now, but should be added}
  1868. ;
  1869. {$ifdef OMFOBJSUPPORT}
  1870. asd_omf_linnum_line:
  1871. { ignore for now, but should be added}
  1872. ;
  1873. {$endif OMFOBJSUPPORT}
  1874. asd_cpu:
  1875. begin
  1876. ObjData.CPUType:=cpu_none;
  1877. for cpu:=low(tcputype) to high(tcputype) do
  1878. if cputypestr[cpu]=tai_directive(hp).name then
  1879. begin
  1880. ObjData.CPUType:=cpu;
  1881. break;
  1882. end;
  1883. end;
  1884. else
  1885. internalerror(2010011102);
  1886. end;
  1887. end;
  1888. ait_eabi_attribute :
  1889. begin
  1890. eabi_section:=ObjData.findsection('.ARM.attributes');
  1891. if not(assigned(eabi_section)) then
  1892. Internalerror(2019100702);
  1893. if eabi_section.Size=0 then
  1894. eabi_section.alloc(16);
  1895. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).tag));
  1896. case tai_eabi_attribute(hp).eattr_typ of
  1897. eattrtype_dword:
  1898. eabi_section.alloc(LengthUleb128(tai_eabi_attribute(hp).value));
  1899. eattrtype_ntbs:
  1900. if assigned(tai_eabi_attribute(hp).valuestr) then
  1901. eabi_section.alloc(Length(tai_eabi_attribute(hp).valuestr^)+1)
  1902. else
  1903. eabi_section.alloc(1);
  1904. else
  1905. Internalerror(2019100703);
  1906. end;
  1907. end;
  1908. else
  1909. ;
  1910. end;
  1911. hp:=Tai(hp.next);
  1912. end;
  1913. TreePass1:=hp;
  1914. end;
  1915. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1916. var
  1917. fillbuffer : tfillbuffer;
  1918. leblen : byte;
  1919. lebbuf : array[0..63] of byte;
  1920. objsym,
  1921. ref,
  1922. objsymend : TObjSymbol;
  1923. zerobuf : array[0..63] of byte;
  1924. relative_reloc: boolean;
  1925. pdata : pointer;
  1926. ssingle : single;
  1927. ddouble : double;
  1928. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  1929. eextended : extended;
  1930. {$else}
  1931. {$ifdef FPC_SOFT_FPUX80}
  1932. f32 : float32;
  1933. f64 : float64;
  1934. eextended : floatx80;
  1935. {$endif}
  1936. {$endif}
  1937. ccomp : comp;
  1938. tmp : word;
  1939. cpu: tcputype;
  1940. ddword : dword;
  1941. eabi_section: TObjSection;
  1942. s: String;
  1943. TmpDataPos: TObjSectionOfs;
  1944. begin
  1945. fillchar(zerobuf,sizeof(zerobuf),0);
  1946. fillchar(objsym,sizeof(objsym),0);
  1947. fillchar(objsymend,sizeof(objsymend),0);
  1948. { main loop }
  1949. while assigned(hp) do
  1950. begin
  1951. {$ifdef DEBUG}
  1952. if not(hp.typ in SkipLineInfo) then
  1953. current_filepos:=tailineinfo(hp).fileinfo;
  1954. {$endif DEBUG}
  1955. case hp.typ of
  1956. ait_align :
  1957. begin
  1958. if tai_align_abstract(hp).aligntype>ObjData.CurrObjSec.secalign then
  1959. InternalError(2012072301);
  1960. if oso_data in ObjData.CurrObjSec.secoptions then
  1961. ObjData.writebytes(Tai_align_abstract(hp).calculatefillbuf(fillbuffer,oso_executable in ObjData.CurrObjSec.secoptions)^,
  1962. Tai_align_abstract(hp).fillsize)
  1963. else
  1964. ObjData.alloc(Tai_align_abstract(hp).fillsize);
  1965. end;
  1966. ait_section :
  1967. begin
  1968. { use cached value }
  1969. ObjData.setsection(Tai_section(hp).sec);
  1970. end;
  1971. ait_symbol :
  1972. begin
  1973. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_symbol(hp).sym));
  1974. end;
  1975. ait_symbol_end :
  1976. begin
  1977. { recalculate size, as some preceding instructions
  1978. could have been changed to smaller size }
  1979. objsym:=ObjData.SymbolRef(Tai_symbol_end(hp).sym);
  1980. objsym.size:=ObjData.CurrObjSec.Size-objsym.offset;
  1981. end;
  1982. ait_datablock :
  1983. begin
  1984. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_datablock(hp).sym));
  1985. {$ifdef USE_COMM_IN_BSS}
  1986. if not(writingpackages and
  1987. Tai_datablock(hp).is_global) then
  1988. {$endif USE_COMM_IN_BSS}
  1989. begin
  1990. ObjData.allocalign(used_align(size_2_align(Tai_datablock(hp).size),0,ObjData.CurrObjSec.secalign));
  1991. ObjData.alloc(Tai_datablock(hp).size);
  1992. end;
  1993. end;
  1994. ait_realconst:
  1995. begin
  1996. case tai_realconst(hp).realtyp of
  1997. aitrealconst_s32bit:
  1998. begin
  1999. ssingle:=single(tai_realconst(hp).value.s32val);
  2000. pdata:=@ssingle;
  2001. end;
  2002. aitrealconst_s64bit:
  2003. begin
  2004. ddouble:=double(tai_realconst(hp).value.s64val);
  2005. pdata:=@ddouble;
  2006. end;
  2007. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  2008. { can't write full 80 bit floating point constants yet on non-x86 }
  2009. aitrealconst_s80bit:
  2010. begin
  2011. eextended:=extended(tai_realconst(hp).value.s80val);
  2012. pdata:=@eextended;
  2013. end;
  2014. {$else}
  2015. {$ifdef FPC_SOFT_FPUX80}
  2016. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  2017. aitrealconst_s80bit:
  2018. begin
  2019. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  2020. begin
  2021. f64:=float64(double(tai_realconst(hp).value.s80val));
  2022. if float64_is_signaling_nan(f64)<>0 then
  2023. begin
  2024. f64.low := 0;
  2025. f64.high := longword($fff80000);
  2026. end;
  2027. eextended:=float64_to_floatx80(f64);
  2028. end
  2029. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  2030. begin
  2031. f32:=float32(single(tai_realconst(hp).value.s80val));
  2032. if float32_is_signaling_nan(f32)<>0 then
  2033. begin
  2034. f32 := longword($ffc00000);
  2035. end;
  2036. eextended:=float32_to_floatx80(f32);
  2037. end
  2038. else
  2039. internalerror(2017091903);
  2040. pdata:=@eextended;
  2041. end;
  2042. {$pop}
  2043. {$endif}
  2044. {$endif cpuextended}
  2045. aitrealconst_s64comp:
  2046. begin
  2047. ccomp:=comp(tai_realconst(hp).value.s64compval);
  2048. pdata:=@ccomp;
  2049. end;
  2050. else
  2051. internalerror(2015030501);
  2052. end;
  2053. ObjData.writebytes(pdata^,tai_realconst(hp).datasize);
  2054. ObjData.writebytes(zerobuf,tai_realconst(hp).savesize-tai_realconst(hp).datasize);
  2055. end;
  2056. ait_string :
  2057. ObjData.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  2058. ait_const :
  2059. begin
  2060. { Recalculate relative symbols, addresses of forward references
  2061. can be changed in treepass1 }
  2062. relative_reloc:=false;
  2063. if assigned(tai_const(hp).sym) and
  2064. assigned(tai_const(hp).endsym) then
  2065. begin
  2066. objsym:=Objdata.SymbolRef(tai_const(hp).sym);
  2067. objsymend:=Objdata.SymbolRef(tai_const(hp).endsym);
  2068. relative_reloc:=(objsym.objsection<>objsymend.objsection);
  2069. if Tai_const(hp).consttype in [aitconst_gottpoff] then
  2070. begin
  2071. if objsymend.objsection<>ObjData.CurrObjSec then
  2072. Internalerror(2019092802);
  2073. Tai_const(hp).value:=objsymend.address-ObjData.CurrObjSec.Size+Tai_const(hp).symofs;
  2074. end
  2075. else if Tai_const(hp).consttype in [aitconst_tlsgd,aitconst_tlsdesc] then
  2076. begin
  2077. if objsymend.objsection<>ObjData.CurrObjSec then
  2078. Internalerror(2019092803);
  2079. Tai_const(hp).value:=ObjData.CurrObjSec.Size-objsymend.address+Tai_const(hp).symofs;
  2080. end
  2081. else if objsymend.objsection<>objsym.objsection then
  2082. begin
  2083. if (Tai_const(hp).consttype in [aitconst_uleb128bit,aitconst_sleb128bit]) or
  2084. (objsym.objsection<>ObjData.CurrObjSec) then
  2085. internalerror(2019010301);
  2086. end
  2087. else
  2088. {$push} {$R-}{$Q-}
  2089. Tai_const(hp).value:=objsymend.address-objsym.address+Tai_const(hp).symofs;
  2090. end;
  2091. {$pop}
  2092. case tai_const(hp).consttype of
  2093. aitconst_64bit,
  2094. aitconst_32bit,
  2095. aitconst_16bit,
  2096. aitconst_64bit_unaligned,
  2097. aitconst_32bit_unaligned,
  2098. aitconst_16bit_unaligned,
  2099. aitconst_8bit :
  2100. begin
  2101. if assigned(tai_const(hp).sym) and
  2102. not assigned(tai_const(hp).endsym) then
  2103. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_ABSOLUTE)
  2104. else if relative_reloc then
  2105. ObjData.writereloc(ObjData.CurrObjSec.size+tai_const(hp).size-objsym.address+tai_const(hp).symofs,tai_const(hp).size,objsymend,RELOC_RELATIVE)
  2106. else
  2107. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2108. end;
  2109. aitconst_rva_symbol :
  2110. begin
  2111. { PE32+? }
  2112. if target_info.system in systems_peoptplus then
  2113. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA)
  2114. else
  2115. ObjData.writereloc(Tai_const(hp).symofs,sizeof(pint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_RVA);
  2116. end;
  2117. aitconst_secrel32_symbol :
  2118. begin
  2119. { Required for DWARF2 support under Windows }
  2120. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_SECREL32);
  2121. end;
  2122. {$ifdef i8086}
  2123. aitconst_farptr :
  2124. if assigned(tai_const(hp).sym) and
  2125. not assigned(tai_const(hp).endsym) then
  2126. ObjData.writereloc(Tai_const(hp).symofs,tai_const(hp).size,Objdata.SymbolRef(tai_const(hp).sym),RELOC_FARPTR)
  2127. else if relative_reloc then
  2128. internalerror(2015040601)
  2129. else
  2130. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2131. aitconst_seg:
  2132. if assigned(tai_const(hp).sym) and (tai_const(hp).size=2) then
  2133. ObjData.writereloc(0,2,Objdata.SymbolRef(tai_const(hp).sym),RELOC_SEG)
  2134. else
  2135. internalerror(2015110502);
  2136. aitconst_dgroup:
  2137. ObjData.writereloc(0,2,nil,RELOC_DGROUP);
  2138. aitconst_fardataseg:
  2139. ObjData.writereloc(0,2,nil,RELOC_FARDATASEG);
  2140. {$endif i8086}
  2141. {$ifdef arm}
  2142. aitconst_got:
  2143. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOT32);
  2144. { aitconst_gottpoff:
  2145. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF); }
  2146. aitconst_tpoff:
  2147. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TPOFF);
  2148. aitconst_tlsgd:
  2149. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSGD);
  2150. aitconst_tlsdesc:
  2151. begin
  2152. { must be a relative symbol, thus value being valid }
  2153. if not(assigned(tai_const(hp).sym)) or not(assigned(tai_const(hp).endsym)) then
  2154. Internalerror(2019092904);
  2155. ObjData.writereloc(Tai_const(hp).value,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_TLSDESC);
  2156. end;
  2157. {$endif arm}
  2158. aitconst_dtpoff:
  2159. { so far, the size of dtpoff is fixed to 4 bytes }
  2160. ObjData.writereloc(Tai_const(hp).symofs,4,Objdata.SymbolRef(tai_const(hp).sym),RELOC_DTPOFF);
  2161. aitconst_gotoff_symbol:
  2162. ObjData.writereloc(Tai_const(hp).symofs,sizeof(longint),Objdata.SymbolRef(tai_const(hp).sym),RELOC_GOTOFF);
  2163. aitconst_uleb128bit,
  2164. aitconst_sleb128bit :
  2165. begin
  2166. if Tai_const(hp).fixed_size=0 then
  2167. Internalerror(2019030302);
  2168. if tai_const(hp).consttype=aitconst_uleb128bit then
  2169. leblen:=EncodeUleb128(qword(Tai_const(hp).value),lebbuf,Tai_const(hp).fixed_size)
  2170. else
  2171. leblen:=EncodeSleb128(Tai_const(hp).value,lebbuf,Tai_const(hp).fixed_size);
  2172. if leblen<>tai_const(hp).fixed_size then
  2173. internalerror(200709271);
  2174. ObjData.writebytes(lebbuf,leblen);
  2175. end;
  2176. aitconst_darwin_dwarf_delta32,
  2177. aitconst_darwin_dwarf_delta64:
  2178. ObjData.writebytes(Tai_const(hp).value,tai_const(hp).size);
  2179. aitconst_half16bit,
  2180. aitconst_gs:
  2181. begin
  2182. tmp:=Tai_const(hp).value div 2;
  2183. ObjData.writebytes(tmp,2);
  2184. end;
  2185. else
  2186. internalerror(200603254);
  2187. end;
  2188. end;
  2189. ait_label :
  2190. begin
  2191. { exporting shouldn't be necessary as labels are local,
  2192. but it's better to be on the safe side (PFV) }
  2193. ObjOutput.exportsymbol(ObjData.SymbolRef(Tai_label(hp).labsym));
  2194. end;
  2195. ait_instruction :
  2196. Taicpu(hp).Pass2(ObjData);
  2197. ait_stab :
  2198. WriteStab(Tai_stab(hp).str);
  2199. ait_function_name,
  2200. ait_force_line : ;
  2201. ait_cutobject :
  2202. if SmartAsm then
  2203. break;
  2204. ait_directive :
  2205. begin
  2206. case tai_directive(hp).directive of
  2207. asd_weak_definition,
  2208. asd_weak_reference:
  2209. begin
  2210. objsym:=ObjData.symbolref(tai_directive(hp).name);
  2211. if objsym.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL] then
  2212. objsym.bind:=AB_WEAK_EXTERNAL
  2213. else
  2214. { TODO: should become a weak definition; for now, do
  2215. the same as what was done for ait_weak }
  2216. objsym.bind:=AB_WEAK_EXTERNAL;
  2217. end;
  2218. asd_cpu:
  2219. begin
  2220. ObjData.CPUType:=cpu_none;
  2221. for cpu:=low(tcputype) to high(tcputype) do
  2222. if cputypestr[cpu]=tai_directive(hp).name then
  2223. begin
  2224. ObjData.CPUType:=cpu;
  2225. break;
  2226. end;
  2227. end;
  2228. {$ifdef OMFOBJSUPPORT}
  2229. asd_omf_linnum_line:
  2230. begin
  2231. TOmfObjSection(ObjData.CurrObjSec).LinNumEntries.Add(
  2232. TOmfSubRecord_LINNUM_MsLink_Entry.Create(
  2233. strtoint(tai_directive(hp).name),
  2234. ObjData.CurrObjSec.Size
  2235. ));
  2236. end;
  2237. {$endif OMFOBJSUPPORT}
  2238. else
  2239. ;
  2240. end
  2241. end;
  2242. ait_symbolpair:
  2243. begin
  2244. if tai_symbolpair(hp).kind=spk_set then
  2245. begin
  2246. objsym:=ObjData.symbolref(tai_symbolpair(hp).sym^);
  2247. ref:=objdata.symbolref(tai_symbolpair(hp).value^);
  2248. objsym.offset:=ref.offset;
  2249. objsym.objsection:=ref.objsection;
  2250. {$ifdef arm}
  2251. objsym.ThumbFunc:=ref.ThumbFunc;
  2252. {$endif arm}
  2253. end;
  2254. end;
  2255. {$ifndef DISABLE_WIN64_SEH}
  2256. ait_seh_directive :
  2257. tai_seh_directive(hp).generate_code(objdata);
  2258. {$endif DISABLE_WIN64_SEH}
  2259. ait_eabi_attribute :
  2260. begin
  2261. eabi_section:=ObjData.findsection('.ARM.attributes');
  2262. if not(assigned(eabi_section)) then
  2263. Internalerror(2019100704);
  2264. if eabi_section.Size=0 then
  2265. begin
  2266. s:='A';
  2267. eabi_section.write(s[1],1);
  2268. ddword:=eabi_section.Size-1;
  2269. eabi_section.write(ddword,4);
  2270. s:='aeabi'#0;
  2271. eabi_section.write(s[1],6);
  2272. s:=#1;
  2273. eabi_section.write(s[1],1);
  2274. ddword:=eabi_section.Size-1-4-6-1;
  2275. eabi_section.write(ddword,4);
  2276. end;
  2277. leblen:=EncodeUleb128(tai_eabi_attribute(hp).tag,lebbuf,0);
  2278. eabi_section.write(lebbuf,leblen);
  2279. case tai_eabi_attribute(hp).eattr_typ of
  2280. eattrtype_dword:
  2281. begin
  2282. leblen:=EncodeUleb128(tai_eabi_attribute(hp).value,lebbuf,0);
  2283. eabi_section.write(lebbuf,leblen);
  2284. end;
  2285. eattrtype_ntbs:
  2286. begin
  2287. if assigned(tai_eabi_attribute(hp).valuestr) then
  2288. s:=tai_eabi_attribute(hp).valuestr^+#0
  2289. else
  2290. s:=#0;
  2291. eabi_section.write(s[1],Length(s));
  2292. end
  2293. else
  2294. Internalerror(2019100705);
  2295. end;
  2296. { update size of attributes section, write directly to the dyn. arrays as
  2297. we do not increase the size of section }
  2298. TmpDataPos:=eabi_section.Data.Pos;
  2299. eabi_section.Data.seek(1);
  2300. ddword:=eabi_section.Size-1;
  2301. eabi_section.Data.write(ddword,4);
  2302. eabi_section.Data.seek(12);
  2303. ddword:=eabi_section.Size-1-4-6;
  2304. eabi_section.Data.write(ddword,4);
  2305. eabi_section.Data.Seek(TmpDataPos);
  2306. end;
  2307. else
  2308. ;
  2309. end;
  2310. hp:=Tai(hp.next);
  2311. end;
  2312. TreePass2:=hp;
  2313. end;
  2314. procedure TInternalAssembler.writetree;
  2315. label
  2316. doexit;
  2317. var
  2318. hp : Tai;
  2319. ObjWriter : TObjectWriter;
  2320. begin
  2321. ObjWriter:=TObjectwriter.create;
  2322. ObjOutput:=CObjOutput.Create(ObjWriter);
  2323. ObjData:=ObjOutput.newObjData(ObjFileName);
  2324. { Pass 0 }
  2325. ObjData.currpass:=0;
  2326. ObjData.createsection(sec_code);
  2327. ObjData.beforealloc;
  2328. { start with list 1 }
  2329. currlistidx:=1;
  2330. currlist:=list[currlistidx];
  2331. hp:=Tai(currList.first);
  2332. while assigned(hp) do
  2333. begin
  2334. hp:=TreePass0(hp);
  2335. MaybeNextList(hp);
  2336. end;
  2337. ObjData.afteralloc;
  2338. { leave if errors have occurred }
  2339. if errorcount>0 then
  2340. goto doexit;
  2341. { Pass 1 }
  2342. ObjData.currpass:=1;
  2343. ObjData.resetsections;
  2344. ObjData.beforealloc;
  2345. ObjData.createsection(sec_code);
  2346. { start with list 1 }
  2347. currlistidx:=1;
  2348. currlist:=list[currlistidx];
  2349. hp:=Tai(currList.first);
  2350. while assigned(hp) do
  2351. begin
  2352. hp:=TreePass1(hp);
  2353. MaybeNextList(hp);
  2354. end;
  2355. ObjData.createsection(sec_code);
  2356. ObjData.afteralloc;
  2357. { leave if errors have occurred }
  2358. if errorcount>0 then
  2359. goto doexit;
  2360. { Pass 2 }
  2361. ObjData.currpass:=2;
  2362. ObjData.resetsections;
  2363. ObjData.beforewrite;
  2364. ObjData.createsection(sec_code);
  2365. { start with list 1 }
  2366. currlistidx:=1;
  2367. currlist:=list[currlistidx];
  2368. hp:=Tai(currList.first);
  2369. while assigned(hp) do
  2370. begin
  2371. hp:=TreePass2(hp);
  2372. MaybeNextList(hp);
  2373. end;
  2374. ObjData.createsection(sec_code);
  2375. ObjData.afterwrite;
  2376. { don't write the .o file if errors have occurred }
  2377. if errorcount=0 then
  2378. begin
  2379. { write objectfile }
  2380. ObjOutput.startobjectfile(ObjFileName);
  2381. ObjOutput.writeobjectfile(ObjData);
  2382. end;
  2383. doexit:
  2384. { Cleanup }
  2385. ObjData.free;
  2386. ObjData:=nil;
  2387. ObjWriter.free;
  2388. end;
  2389. procedure TInternalAssembler.writetreesmart;
  2390. var
  2391. hp : Tai;
  2392. startsectype : TAsmSectiontype;
  2393. place: tcutplace;
  2394. ObjWriter : TObjectWriter;
  2395. startsecname: String;
  2396. startsecorder: TAsmSectionOrder;
  2397. begin
  2398. if not(cs_asm_leave in current_settings.globalswitches) and
  2399. not(af_needar in asminfo^.flags) then
  2400. ObjWriter:=CInternalAr.CreateAr(current_module.staticlibfilename)
  2401. else
  2402. ObjWriter:=TObjectwriter.create;
  2403. NextSmartName(cut_normal);
  2404. ObjOutput:=CObjOutput.Create(ObjWriter);
  2405. startsectype:=sec_none;
  2406. startsecname:='';
  2407. startsecorder:=secorder_default;
  2408. { start with list 1 }
  2409. currlistidx:=1;
  2410. currlist:=list[currlistidx];
  2411. hp:=Tai(currList.first);
  2412. while assigned(hp) do
  2413. begin
  2414. ObjData:=ObjOutput.newObjData(ObjFileName);
  2415. { Pass 0 }
  2416. ObjData.currpass:=0;
  2417. ObjData.resetsections;
  2418. ObjData.beforealloc;
  2419. if startsectype<>sec_none then
  2420. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2421. TreePass0(hp);
  2422. ObjData.afteralloc;
  2423. { leave if errors have occurred }
  2424. if errorcount>0 then
  2425. break;
  2426. { Pass 1 }
  2427. ObjData.currpass:=1;
  2428. ObjData.resetsections;
  2429. ObjData.beforealloc;
  2430. if startsectype<>sec_none then
  2431. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2432. TreePass1(hp);
  2433. ObjData.afteralloc;
  2434. { leave if errors have occurred }
  2435. if errorcount>0 then
  2436. break;
  2437. { Pass 2 }
  2438. ObjData.currpass:=2;
  2439. ObjOutput.startobjectfile(ObjFileName);
  2440. ObjData.resetsections;
  2441. ObjData.beforewrite;
  2442. if startsectype<>sec_none then
  2443. ObjData.CreateSection(startsectype,startsecname,startsecorder);
  2444. hp:=TreePass2(hp);
  2445. ObjData.afterwrite;
  2446. { leave if errors have occurred }
  2447. if errorcount>0 then
  2448. break;
  2449. { write the current objectfile }
  2450. ObjOutput.writeobjectfile(ObjData);
  2451. ObjData.free;
  2452. ObjData:=nil;
  2453. { end of lists? }
  2454. if not MaybeNextList(hp) then
  2455. break;
  2456. { we will start a new objectfile so reset everything }
  2457. { The place can still change in the next while loop, so don't init }
  2458. { the writer yet (JM) }
  2459. if (hp.typ=ait_cutobject) then
  2460. place := Tai_cutobject(hp).place
  2461. else
  2462. place := cut_normal;
  2463. { avoid empty files }
  2464. startsectype:=sec_none;
  2465. startsecname:='';
  2466. startsecorder:=secorder_default;
  2467. while assigned(hp) and
  2468. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  2469. begin
  2470. if Tai(hp).typ=ait_section then
  2471. begin
  2472. startsectype:=Tai_section(hp).sectype;
  2473. startsecname:=Tai_section(hp).name^;
  2474. startsecorder:=Tai_section(hp).secorder;
  2475. end;
  2476. if (Tai(hp).typ=ait_cutobject) then
  2477. place:=Tai_cutobject(hp).place;
  2478. hp:=Tai(hp.next);
  2479. end;
  2480. if not MaybeNextList(hp) then
  2481. break;
  2482. { start next objectfile }
  2483. NextSmartName(place);
  2484. end;
  2485. ObjData.free;
  2486. ObjData:=nil;
  2487. ObjWriter.free;
  2488. end;
  2489. procedure TInternalAssembler.MakeObject;
  2490. var to_do:set of TasmlistType;
  2491. i:TasmlistType;
  2492. procedure addlist(p:TAsmList);
  2493. begin
  2494. inc(lists);
  2495. list[lists]:=p;
  2496. end;
  2497. begin
  2498. to_do:=[low(Tasmlisttype)..high(Tasmlisttype)];
  2499. if usedeffileforexports then
  2500. exclude(to_do,al_exports);
  2501. if not(tf_section_threadvars in target_info.flags) then
  2502. exclude(to_do,al_threadvars);
  2503. for i:=low(TasmlistType) to high(TasmlistType) do
  2504. if (i in to_do) and (current_asmdata.asmlists[i]<>nil) and
  2505. (not current_asmdata.asmlists[i].empty) then
  2506. addlist(current_asmdata.asmlists[i]);
  2507. if SmartAsm then
  2508. writetreesmart
  2509. else
  2510. writetree;
  2511. end;
  2512. {*****************************************************************************
  2513. Generate Assembler Files Main Procedure
  2514. *****************************************************************************}
  2515. Procedure GenerateAsm(smart:boolean);
  2516. var
  2517. a : TAssembler;
  2518. begin
  2519. if not assigned(CAssembler[target_asm.id]) then
  2520. Message(asmw_f_assembler_output_not_supported);
  2521. a:=CAssembler[target_asm.id].Create(@target_asm,smart);
  2522. a.MakeObject;
  2523. a.Free;
  2524. end;
  2525. function GetExternalGnuAssemblerWithAsmInfoWriter(info: pasminfo; wr: TExternalAssemblerOutputFile): TExternalAssembler;
  2526. var
  2527. asmkind: tasm;
  2528. begin
  2529. for asmkind in [as_gas,as_ggas,as_darwin,as_clang_gas,as_clang_asdarwin] do
  2530. if assigned(asminfos[asmkind]) and
  2531. (target_info.system in asminfos[asmkind]^.supported_targets) then
  2532. begin
  2533. result:=TExternalAssemblerClass(CAssembler[asmkind]).CreateWithWriter(asminfos[asmkind],wr,false,false);
  2534. exit;
  2535. end;
  2536. Internalerror(2015090604);
  2537. end;
  2538. {*****************************************************************************
  2539. Init/Done
  2540. *****************************************************************************}
  2541. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  2542. var
  2543. t : tasm;
  2544. begin
  2545. t:=r.id;
  2546. if assigned(asminfos[t]) then
  2547. writeln('Warning: Assembler is already registered!')
  2548. else
  2549. Getmem(asminfos[t],sizeof(tasminfo));
  2550. asminfos[t]^:=r;
  2551. CAssembler[t]:=c;
  2552. end;
  2553. end.