assemble.pas 73 KB

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