2
0

assemble.pas 80 KB

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