agx86nsm.pas 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for the Nasm assembler with
  4. Intel syntax for the i386+
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit agx86nsm;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,cpubase,globtype,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  24. type
  25. { TX86NasmAssembler }
  26. TX86NasmAssembler = class(texternalassembler)
  27. strict private
  28. type
  29. { TX86NasmSection }
  30. TX86NasmSection=class(TFPHashObject)
  31. end;
  32. { TX86NasmGroup }
  33. TX86NasmGroup=class(TFPHashObject)
  34. Sections: TFPHashObjectList;
  35. constructor Create(HashObjectList:TFPHashObjectList;const s:TSymStr);
  36. destructor Destroy;override;
  37. end;
  38. private
  39. FSections: TFPHashObjectList;
  40. FGroups: TFPHashObjectList;
  41. {$ifndef i8086}
  42. using_relative : boolean;
  43. {$endif i8086}
  44. function CodeSectionName(const aname:string): string;
  45. procedure WriteReference(var ref : treference);
  46. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  47. procedure WriteOper_jmp(const o:toper; ai : taicpu);
  48. procedure WriteSection(atype:TAsmSectiontype;const aname:string;alignment : longint);
  49. procedure WriteHiddenSymbolAttribute(sym: TAsmSymbol);
  50. procedure ResetSectionsList;
  51. procedure AddGroup(const grpname: string);
  52. procedure AddSegmentToGroup(const grpname,segname: string);
  53. procedure WriteGroup(data:TObject;arg:pointer);
  54. procedure WriteGroups;
  55. protected
  56. function single2str(d: single): string; override;
  57. function double2str(d: double): string; override;
  58. function extended2str(e: extended): string; override;
  59. public
  60. destructor Destroy;override;
  61. procedure WriteTree(p:TAsmList);override;
  62. procedure WriteAsmList;override;
  63. procedure WriteExternals;
  64. procedure WriteSmartExternals;
  65. procedure WriteHeader;
  66. function MakeCmdLine: TCmdStr;override;
  67. end;
  68. implementation
  69. uses
  70. cutils,globals,systems,fpchash,
  71. fmodule,finput,verbose,cpuinfo,cgbase,omfbase
  72. ;
  73. const
  74. line_length = 64;
  75. nasm_regname_table : array[tregisterindex] of string[13] = (
  76. {r386nasm.inc contains the Nasm name of each register.}
  77. {$if defined(x86_64)}
  78. {$i r8664nasm.inc}
  79. {$elseif defined(i386)}
  80. {$i r386nasm.inc}
  81. {$elseif defined(i8086)}
  82. {$i r8086nasm.inc}
  83. {$endif}
  84. );
  85. { nasm 2.13 expects lowercase cpu names }
  86. nasm_cpu_name : array[tcputype] of string = (
  87. {$if defined(x86_64)}
  88. 'ia64', // cpu_none
  89. 'x64', // cpu_athlon64
  90. 'x64', // cpu_x86_64
  91. 'x64', // cpu_x86_64_v1
  92. 'ia64', // cpu_core_i
  93. 'ia64', // cpu_x86_64_v2
  94. 'ia64', // cpu_bobcat
  95. 'ia64', // cpu_core_avx
  96. 'ia64', // cpu_jaguar
  97. 'ia64', // cpu_piledriver
  98. 'ia64', // cpu_excavator
  99. 'ia64', // cpu_core_avx2
  100. 'ia64', // cpu_x86_64_v3
  101. 'ia64', // cpu_zen
  102. 'ia64', // cpu_zen2
  103. 'ia64', // cpu_x86_64_v4
  104. 'ia64', // cpu_skylake-x
  105. 'ia64', // cpu_icelake
  106. 'ia64', // cpu_icelake_client
  107. 'ia64', // cpu_icelake_server
  108. 'ia64', // cpu_zen3
  109. 'ia64', // cpu_zen4
  110. 'ia64' // cpu_zen5
  111. {$elseif defined(i386)}
  112. 'ia64', // cpu_none
  113. '386', // cpu_386
  114. '486', // cpu_486
  115. 'pentium', // cpu_Pentium
  116. 'p2', // cpu_Pentium2
  117. 'p3', // cpu_Pentium3
  118. 'p4', // cpu_Pentium4
  119. 'p4', // cpu_PentiumM
  120. 'ia64', // cpu_core_i
  121. 'ia64', // cpu_bobcat
  122. 'ia64', // cpu_core_avx
  123. 'ia64', // cpu_jaguar
  124. 'ia64', // cpu_piledriver
  125. 'ia64', // cpu_excavator
  126. 'ia64', // cpu_core_avx2
  127. 'ia64', // cpu_zen
  128. 'ia64', // cpu_zen2
  129. 'ia64', // cpu_skylake_x
  130. 'ia64', // cpu_icelake
  131. 'ia64', // cpu_icelake_client
  132. 'ia64', // cpu_icelake_server
  133. 'ia64', // cpu_zen3
  134. 'ia64', // cpu_zen4
  135. 'ia64' // cpu_zen5
  136. {$elseif defined(i8086)}
  137. 'ia64', // cpu_none
  138. '8086', // cpu_8086
  139. '186', // cpu_186
  140. '286', // cpu_286
  141. '386', // cpu_386
  142. '486', // cpu_486
  143. 'pentium', // cpu_Pentium
  144. 'p2', // cpu_Pentium2
  145. 'p3', // cpu_Pentium3
  146. 'p4', // cpu_Pentium4
  147. 'p4' // cpu_PentiumM
  148. {$endif}
  149. );
  150. function nasm_regname(r:Tregister):string;
  151. var
  152. p : tregisterindex;
  153. begin
  154. p:=findreg_by_number(r);
  155. if p<>0 then
  156. result:=nasm_regname_table[p]
  157. else
  158. result:=generic_regname(r);
  159. end;
  160. function TX86NasmAssembler.single2str(d: single): string;
  161. var
  162. hs : string;
  163. p : longint;
  164. begin
  165. str(d,hs);
  166. { nasm expects a lowercase e }
  167. p:=pos('E',hs);
  168. if p>0 then
  169. hs[p]:='e';
  170. p:=pos('+',hs);
  171. if p>0 then
  172. delete(hs,p,1);
  173. single2str:=lower(hs);
  174. end;
  175. function TX86NasmAssembler.double2str(d: double): string;
  176. var
  177. hs : string;
  178. p : longint;
  179. begin
  180. str(d,hs);
  181. { nasm expects a lowercase e }
  182. p:=pos('E',hs);
  183. if p>0 then
  184. hs[p]:='e';
  185. p:=pos('+',hs);
  186. if p>0 then
  187. delete(hs,p,1);
  188. double2str:=lower(hs);
  189. end;
  190. function TX86NasmAssembler.extended2str(e: extended): string;
  191. var
  192. hs : string;
  193. p : longint;
  194. begin
  195. str(e,hs);
  196. { nasm expects a lowercase e }
  197. p:=pos('E',hs);
  198. if p>0 then
  199. hs[p]:='e';
  200. p:=pos('+',hs);
  201. if p>0 then
  202. delete(hs,p,1);
  203. extended2str:=lower(hs);
  204. end;
  205. destructor TX86NasmAssembler.Destroy;
  206. begin
  207. FSections.Free;
  208. FGroups.Free;
  209. inherited Destroy;
  210. end;
  211. function sizestr(s:topsize;dest:boolean):string;
  212. begin
  213. case s of
  214. S_B : sizestr:='byte ';
  215. S_W : sizestr:='word ';
  216. S_L : sizestr:='dword ';
  217. S_Q : sizestr:='qword ';
  218. S_IS : sizestr:='word ';
  219. S_IL : sizestr:='dword ';
  220. S_IQ : sizestr:='qword ';
  221. S_FS : sizestr:='dword ';
  222. S_FL : sizestr:='qword ';
  223. S_FX : sizestr:='tword ';
  224. S_BW : if dest then
  225. sizestr:='word '
  226. else
  227. sizestr:='byte ';
  228. S_BL : if dest then
  229. sizestr:='dword '
  230. else
  231. sizestr:='byte ';
  232. S_WL : if dest then
  233. sizestr:='dword '
  234. else
  235. sizestr:='word ';
  236. {$ifdef x86_64}
  237. S_BQ : if dest then
  238. sizestr:='qword '
  239. else
  240. sizestr:='byte ';
  241. S_WQ : if dest then
  242. sizestr:='qword '
  243. else
  244. sizestr:='word ';
  245. S_LQ : if dest then
  246. sizestr:='qword '
  247. else
  248. sizestr:='dword ';
  249. { Nothing needed for XMM registers }
  250. S_XMM: sizestr:='';
  251. {$endif x86_64}
  252. else { S_NO }
  253. sizestr:='';
  254. end;
  255. end;
  256. Function PadTabs(const p:string;addch:char):string;
  257. var
  258. s : string;
  259. i : longint;
  260. begin
  261. i:=length(p);
  262. if addch<>#0 then
  263. begin
  264. inc(i);
  265. s:=p+addch;
  266. end
  267. else
  268. s:=p;
  269. if i<8 then
  270. PadTabs:=s+#9#9
  271. else
  272. PadTabs:=s+#9;
  273. end;
  274. {****************************************************************************
  275. TX86NasmAssembler.TX86NasmGroup
  276. ****************************************************************************}
  277. constructor TX86NasmAssembler.TX86NasmGroup.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  278. begin
  279. inherited;
  280. Sections:=TFPHashObjectList.Create;
  281. end;
  282. destructor TX86NasmAssembler.TX86NasmGroup.Destroy;
  283. begin
  284. Sections.Free;
  285. inherited Destroy;
  286. end;
  287. {****************************************************************************
  288. TX86NasmAssembler
  289. ****************************************************************************}
  290. function TX86NasmAssembler.CodeSectionName(const aname:string): string;
  291. begin
  292. {$ifdef i8086}
  293. if current_settings.x86memorymodel in x86_far_code_models then
  294. begin
  295. if cs_huge_code in current_settings.moduleswitches then
  296. result:=TrimStrCRC32(aname,30) + '_TEXT'
  297. else
  298. result:=current_module.modulename^ + '_TEXT';
  299. end
  300. else
  301. result:='_TEXT';
  302. {$else i8086}
  303. result:='.text';
  304. {$endif}
  305. end;
  306. procedure TX86NasmAssembler.WriteReference(var ref : treference);
  307. var
  308. first : boolean;
  309. base_done : boolean;
  310. begin
  311. with ref do
  312. begin
  313. writer.AsmWrite('[');
  314. first:=true;
  315. base_done:=false;
  316. if (segment<>NR_NO) then
  317. writer.AsmWrite(nasm_regname(segment)+':');
  318. {$ifdef x86_64}
  319. if (base=NR_RIP) then
  320. begin
  321. { nasm RIP is implicit for pic }
  322. if not (ref.refaddr in [addr_pic,addr_pic_no_got]) and not using_relative then
  323. writer.AsmWrite('$ + ');
  324. base_done:=true;
  325. end;
  326. {$endif x86_64}
  327. if assigned(symbol) then
  328. begin
  329. writer.AsmWrite(ApplyAsmSymbolRestrictions(symbol.name));
  330. if SmartAsm then
  331. AddSymbol(symbol.name,false);
  332. first:=false;
  333. end;
  334. if (base<>NR_NO) and not base_done then
  335. begin
  336. if not(first) then
  337. writer.AsmWrite('+')
  338. else
  339. first:=false;
  340. writer.AsmWrite(nasm_regname(base))
  341. end;
  342. if (index<>NR_NO) then
  343. begin
  344. if not(first) then
  345. writer.AsmWrite('+')
  346. else
  347. first:=false;
  348. writer.AsmWrite(nasm_regname(index));
  349. if scalefactor<>0 then
  350. writer.AsmWrite('*'+tostr(scalefactor));
  351. end;
  352. if offset<0 then
  353. begin
  354. writer.AsmWrite(tostr(offset));
  355. first:=false;
  356. end
  357. else if (offset>0) then
  358. begin
  359. writer.AsmWrite('+'+tostr(offset));
  360. first:=false;
  361. end;
  362. if first then
  363. writer.AsmWrite('0');
  364. writer.AsmWrite(']');
  365. end;
  366. end;
  367. procedure TX86NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  368. begin
  369. case o.typ of
  370. top_reg :
  371. writer.AsmWrite(nasm_regname(o.reg));
  372. top_const :
  373. begin
  374. if (ops=1) and (opcode<>A_RET) then
  375. writer.AsmWrite(sizestr(s,dest));
  376. writer.AsmWrite(tostr(o.val));
  377. end;
  378. top_ref :
  379. begin
  380. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  381. begin
  382. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  383. (opcode = A_LSS) or (opcode = A_LFS) or
  384. {$ifndef x86_64}
  385. (opcode = A_LES) or (opcode = A_LDS) or
  386. {$endif x86_64}
  387. // (opcode = A_SHR) or (opcode = A_SHL) or
  388. // (opcode = A_SAR) or (opcode = A_SAL) or
  389. (opcode = A_OUT) or (opcode = A_IN)) then
  390. writer.AsmWrite(sizestr(s,dest));
  391. WriteReference(o.ref^);
  392. end
  393. {$ifdef i8086}
  394. else if o.ref^.refaddr=addr_dgroup then
  395. begin
  396. writer.AsmWrite('DGROUP');
  397. { Make sure GROUP DGROUP is generated }
  398. AddGroup('DGROUP');
  399. end
  400. else if o.ref^.refaddr=addr_fardataseg then
  401. begin
  402. writer.AsmWrite(current_module.modulename^+'_DATA');
  403. end
  404. {$endif i8086}
  405. else
  406. begin
  407. {$ifdef x86_64}
  408. if s=S_L then
  409. writer.AsmWrite('dword ')
  410. else
  411. writer.AsmWrite('qword ');
  412. {$endif}
  413. {$ifdef i386}
  414. writer.AsmWrite('dword ');
  415. {$endif i386}
  416. {$ifdef i8086}
  417. if o.ref^.refaddr=addr_seg then
  418. writer.AsmWrite('SEG ')
  419. else
  420. writer.AsmWrite('word ');
  421. {$endif i8086}
  422. if assigned(o.ref^.symbol) then
  423. begin
  424. if SmartAsm then
  425. AddSymbol(o.ref^.symbol.name,false);
  426. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  427. if o.ref^.offset=0 then
  428. exit;
  429. end;
  430. if o.ref^.offset>0 then
  431. writer.AsmWrite('+');
  432. writer.AsmWrite(tostr(o.ref^.offset));
  433. end;
  434. end;
  435. else
  436. internalerror(2020100812);
  437. end;
  438. end;
  439. procedure TX86NasmAssembler.WriteOper_jmp(const o:toper; ai : taicpu);
  440. begin
  441. case o.typ of
  442. top_reg :
  443. writer.AsmWrite(nasm_regname(o.reg));
  444. top_ref :
  445. if o.ref^.refaddr=addr_no then
  446. begin
  447. if ai.opsize=S_FAR then
  448. writer.AsmWrite('far ');
  449. WriteReference(o.ref^);
  450. end
  451. else
  452. begin
  453. if ai.opsize=S_FAR then
  454. writer.AsmWrite('far ');
  455. { else
  456. writer.AsmWrite('near ') just disables short branches, increasing code size.
  457. Omitting it does not cause any bad effects, tested with nasm 2.11. }
  458. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  459. if SmartAsm then
  460. AddSymbol(o.ref^.symbol.name,false);
  461. if o.ref^.offset>0 then
  462. writer.AsmWrite('+'+tostr(o.ref^.offset))
  463. else
  464. if o.ref^.offset<0 then
  465. writer.AsmWrite(tostr(o.ref^.offset));
  466. end;
  467. top_const :
  468. writer.AsmWrite(tostr(aint(o.val)));
  469. else
  470. internalerror(2020100813);
  471. end;
  472. end;
  473. const
  474. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[30]=(
  475. #9'FIXME_128BIT'#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  476. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  477. #9'RVA'#9,#9'SECREL32'#9,#9'FIXME_darwin_dwarf_delta64'#9,
  478. #9'FIXME_darwin_dwarf_delta32'#9,#9'FIXME_half16bit'#9,#9'FIXME_gs'#9,
  479. #9'DW'#9,#9'DD'#9,#9'FIXME_64BIT_UNALIGNED'#9
  480. );
  481. procedure TX86NasmAssembler.WriteSection(atype : TAsmSectiontype;
  482. const aname : string; alignment : longint);
  483. const
  484. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  485. '.text',
  486. '.data',
  487. '.data',
  488. '.rodata',
  489. '.bss',
  490. '.tbss',
  491. '.pdata',
  492. '.text','.data','.data','.data','.data',
  493. '.stab',
  494. '.stabstr',
  495. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  496. '.eh_frame',
  497. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges','.debug_loc','.debug_loclists',
  498. '.fpc',
  499. '',
  500. '.init',
  501. '.fini',
  502. '.objc_class',
  503. '.objc_meta_class',
  504. '.objc_cat_cls_meth',
  505. '.objc_cat_inst_meth',
  506. '.objc_protocol',
  507. '.objc_string_object',
  508. '.objc_cls_meth',
  509. '.objc_inst_meth',
  510. '.objc_cls_refs',
  511. '.objc_message_refs',
  512. '.objc_symbols',
  513. '.objc_category',
  514. '.objc_class_vars',
  515. '.objc_instance_vars',
  516. '.objc_module_info',
  517. '.objc_class_names',
  518. '.objc_meth_var_types',
  519. '.objc_meth_var_names',
  520. '.objc_selector_strs',
  521. '.objc_protocol_ext',
  522. '.objc_class_ext',
  523. '.objc_property',
  524. '.objc_image_info',
  525. '.objc_cstring_object',
  526. '.objc_sel_fixup',
  527. '__DATA,__objc_data',
  528. '__DATA,__objc_const',
  529. '.objc_superrefs',
  530. '__DATA, __datacoal_nt,coalesced',
  531. '.objc_classlist',
  532. '.objc_nlclasslist',
  533. '.objc_catlist',
  534. '.obcj_nlcatlist',
  535. '.objc_protolist',
  536. '.stack',
  537. '.heap',
  538. ',gcc_except_table',
  539. ',ARM_attributes',
  540. ',note'
  541. );
  542. var
  543. secname,secgroup: string;
  544. begin
  545. writer.AsmLn;
  546. writer.AsmWrite('SECTION ');
  547. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  548. Thus, data which normally goes into .rodata and .rodata_norel sections must
  549. end up in .data section }
  550. if (atype in [sec_rodata,sec_rodata_norel]) and
  551. (target_info.system=system_i386_go32v2) then
  552. writer.AsmWrite('.data')
  553. else if (atype=sec_threadvar) and
  554. (target_info.system in (systems_windows+systems_wince)) then
  555. writer.AsmWrite('.tls'#9'bss')
  556. else if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
  557. begin
  558. if (atype=sec_user) then
  559. secname:=aname
  560. else if secnames[atype]='.text' then
  561. secname:=CodeSectionName(aname)
  562. else if omf_segclass(atype)='FAR_DATA' then
  563. secname:=current_module.modulename^ + '_DATA'
  564. else
  565. secname:=omf_secnames[atype];
  566. writer.AsmWrite(secname);
  567. { first use of this section in the object file? }
  568. if FSections.Find(secname)=nil then
  569. begin
  570. { yes -> write the section attributes as well }
  571. if atype=sec_stack then
  572. writer.AsmWrite(' stack');
  573. if atype in [sec_debug_frame,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges] then
  574. writer.AsmWrite(' use32')
  575. else
  576. writer.AsmWrite(' use16');
  577. writer.AsmWrite(' class='+omf_segclass(atype)+
  578. ' align='+tostr(omf_sectiontype2align(atype)));
  579. TX86NasmSection.Create(FSections,secname);
  580. secgroup:=omf_section_primary_group(atype,aname);
  581. if secgroup<>'' then
  582. AddSegmentToGroup(secgroup,secname);
  583. end;
  584. end
  585. else if (atype=sec_user) then
  586. writer.AsmWrite(aname)
  587. else if secnames[atype]='.text' then
  588. writer.AsmWrite(CodeSectionName(aname))
  589. else
  590. writer.AsmWrite(secnames[atype]);
  591. if create_smartlink_sections and
  592. (atype<>sec_bss) and
  593. (aname<>'') then
  594. begin
  595. writer.AsmWrite('.');
  596. writer.AsmWrite(aname);
  597. if atype in [sec_init, sec_fini, sec_stub, sec_code] then
  598. writer.AsmWrite(' code align='+tostr(alignment))
  599. else if atype in [sec_rodata, sec_rodata_norel] then
  600. writer.AsmWrite(' rdata align='+tostr(alignment))
  601. else
  602. writer.AsmWrite(' data align='+tostr(alignment))
  603. end;
  604. writer.AsmLn;
  605. LastSecType:=atype;
  606. end;
  607. procedure TX86NasmAssembler.WriteHiddenSymbolAttribute(sym: TAsmSymbol);
  608. begin
  609. if target_info.system in systems_windows then
  610. exit;
  611. if target_info.system in systems_darwin then
  612. writer.AsmWrite(':private_extern')
  613. else
  614. case sym.typ of
  615. AT_FUNCTION:
  616. writer.AsmWrite(':function hidden');
  617. AT_DATA:
  618. writer.AsmWrite(':data hidden');
  619. else
  620. Internalerror(2020111301);
  621. end;
  622. end;
  623. procedure TX86NasmAssembler.ResetSectionsList;
  624. begin
  625. FSections.Free;
  626. FSections:=TFPHashObjectList.Create;
  627. FGroups.Free;
  628. FGroups:=TFPHashObjectList.Create;
  629. end;
  630. procedure TX86NasmAssembler.AddGroup(const grpname: string);
  631. begin
  632. if FGroups.Find(grpname)=nil then
  633. TX86NasmGroup.Create(FGroups,grpname);
  634. end;
  635. procedure TX86NasmAssembler.AddSegmentToGroup(const grpname, segname: string);
  636. var
  637. grp: TX86NasmGroup;
  638. begin
  639. grp:=TX86NasmGroup(FGroups.Find(grpname));
  640. if grp=nil then
  641. grp:=TX86NasmGroup.Create(FGroups,grpname);
  642. TX86NasmSection.Create(grp.Sections,segname);
  643. end;
  644. procedure TX86NasmAssembler.WriteGroup(data: TObject; arg: pointer);
  645. var
  646. grp: TX86NasmGroup;
  647. i: Integer;
  648. begin
  649. grp:=TX86NasmGroup(data);
  650. writer.AsmWrite('GROUP '+grp.Name);
  651. for i:=0 to grp.Sections.Count-1 do
  652. writer.AsmWrite(' '+grp.Sections.NameOfIndex(i));
  653. writer.AsmLn;
  654. end;
  655. procedure TX86NasmAssembler.WriteGroups;
  656. begin
  657. {$ifdef i8086}
  658. if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
  659. begin
  660. if current_settings.x86memorymodel=mm_huge then
  661. WriteSection(sec_data,'',2);
  662. writer.AsmLn;
  663. FGroups.ForEachCall(@WriteGroup,nil);
  664. end;
  665. {$endif i8086}
  666. end;
  667. procedure TX86NasmAssembler.WriteTree(p:TAsmList);
  668. {$ifdef cpuextended}
  669. type
  670. t80bitarray = array[0..9] of byte;
  671. {$endif cpuextended}
  672. var
  673. s : string;
  674. hp : tai;
  675. counter,
  676. lines,
  677. i,j,l : longint;
  678. InlineLevel : longint;
  679. consttype : taiconst_type;
  680. do_line, SkipNewLine,
  681. quoted : boolean;
  682. fixed_opcode: TAsmOp;
  683. prefix, LastSecName : string;
  684. LastAlign : LongInt;
  685. cpu: tcputype;
  686. prevfileinfo : tfileposinfo;
  687. previnfile : tinputfile;
  688. NewObject : boolean;
  689. begin
  690. if not assigned(p) then
  691. exit;
  692. InlineLevel:=0;
  693. NewObject:=true;
  694. { lineinfo is only needed for al_procedures (PFV) }
  695. do_line:=(cs_asm_source in current_settings.globalswitches) or
  696. ((cs_lineinfo in current_settings.moduleswitches)
  697. and (p=current_asmdata.asmlists[al_procedures]));
  698. hp:=tai(p.first);
  699. while assigned(hp) do
  700. begin
  701. prefetch(pointer(hp.next)^);
  702. if not(hp.typ in SkipLineInfo) then
  703. begin
  704. previnfile:=lastinfile;
  705. prevfileinfo:=lastfileinfo;
  706. current_filepos:=tailineinfo(hp).fileinfo;
  707. { no line info for inlined code }
  708. if do_line and (inlinelevel=0) then
  709. WriteSourceLine(hp as tailineinfo);
  710. if (lastfileinfo.line<>prevfileinfo.line) or
  711. (previnfile<>lastinfile) then
  712. begin
  713. { +0 postfix means no line increment per assembler instruction }
  714. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  715. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  716. writer.AsmWriteLn(' '+lastinfile.name)
  717. else
  718. writer.AsmLn;
  719. NewObject:=false;
  720. end;
  721. end;
  722. case hp.typ of
  723. ait_section :
  724. begin
  725. ResetSourceLines;
  726. if tai_section(hp).sectype<>sec_none then
  727. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secalign);
  728. LastSecType:=tai_section(hp).sectype;
  729. end;
  730. ait_align :
  731. begin
  732. if (tai_align(hp).aligntype>1) then
  733. begin
  734. if (LastSecType=sec_bss) or (
  735. (LastSecType=sec_threadvar) and
  736. (target_info.system in (systems_windows+systems_wince))
  737. ) then
  738. writer.AsmWriteLn(#9'ALIGNB '+tostr(tai_align(hp).aligntype))
  739. else if tai_align_abstract(hp).use_op then
  740. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB '+tostr(tai_align_abstract(hp).fillop))
  741. else if LastSecType in [sec_code,sec_stub,sec_init,sec_fini] then
  742. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype))
  743. else
  744. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB 0');
  745. end;
  746. end;
  747. ait_datablock :
  748. begin
  749. if tai_datablock(hp).is_global or SmartAsm then
  750. begin
  751. writer.AsmWrite(#9'GLOBAL ');
  752. writer.AsmWrite(tai_datablock(hp).sym.name);
  753. if tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN then
  754. WriteHiddenSymbolAttribute(tai_datablock(hp).sym);
  755. writer.AsmLn;
  756. end;
  757. writer.AsmWrite(PadTabs(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name),':'));
  758. if SmartAsm then
  759. AddSymbol(tai_datablock(hp).sym.name,true);
  760. writer.AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  761. end;
  762. ait_const:
  763. begin
  764. consttype:=tai_const(hp).consttype;
  765. case consttype of
  766. aitconst_uleb128bit:
  767. writer.AsmWriteLn(ait_const2str[aitconst_8bit]+uleb128tostr(qword(tai_const(hp).value)));
  768. aitconst_sleb128bit:
  769. writer.AsmWriteLn(ait_const2str[aitconst_8bit]+sleb128tostr(tai_const(hp).value));
  770. aitconst_128bit:
  771. writer.AsmWriteLn(asminfo^.comment+'Unsupported const type '+
  772. ait_const2str[consttype]);
  773. {$ifdef i8086}
  774. aitconst_farptr:
  775. begin
  776. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  777. if assigned(tai_const(hp).sym) then
  778. begin
  779. if SmartAsm then
  780. AddSymbol(tai_const(hp).sym.name,false);
  781. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_const(hp).sym.name));
  782. if tai_const(hp).value<>0 then
  783. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  784. writer.AsmLn;
  785. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  786. writer.AsmWrite('SEG ');
  787. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_const(hp).sym.name));
  788. end
  789. else
  790. writer.AsmWrite(tostr(lo(longint(tai_const(hp).value)))+','+
  791. tostr(hi(longint(tai_const(hp).value))));
  792. writer.AsmLn;
  793. end;
  794. aitconst_seg:
  795. begin
  796. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  797. if assigned(tai_const(hp).sym) then
  798. begin
  799. if SmartAsm then
  800. AddSymbol(tai_const(hp).sym.name,false);
  801. writer.AsmWrite('SEG ');
  802. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_const(hp).sym.name));
  803. end
  804. else
  805. internalerror(2015110501);
  806. writer.AsmLn;
  807. end;
  808. aitconst_dgroup:
  809. writer.AsmWriteLn(#9'DW'#9'DGROUP');
  810. aitconst_fardataseg:
  811. writer.AsmWriteLn(#9'DW'#9+current_module.modulename^+'_DATA');
  812. {$endif i8086}
  813. {$ifdef x86_64}
  814. aitconst_rva_symbol,
  815. aitconst_secrel32_symbol: ;
  816. {$endif x86_64}
  817. {$ifdef i386}
  818. aitconst_rva_symbol,
  819. aitconst_secrel32_symbol,
  820. {$endif i386}
  821. aitconst_64bit,
  822. aitconst_32bit,
  823. aitconst_16bit,
  824. aitconst_8bit,
  825. aitconst_16bit_unaligned,
  826. aitconst_32bit_unaligned,
  827. aitconst_64bit_unaligned:
  828. begin
  829. writer.AsmWrite(ait_const2str[tai_const(hp).consttype]);
  830. l:=0;
  831. repeat
  832. if assigned(tai_const(hp).sym) then
  833. begin
  834. if SmartAsm then
  835. begin
  836. AddSymbol(tai_const(hp).sym.name,false);
  837. if assigned(tai_const(hp).endsym) then
  838. AddSymbol(tai_const(hp).endsym.name,false);
  839. end;
  840. if assigned(tai_const(hp).endsym) then
  841. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  842. else
  843. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  844. if tai_const(hp).value<>0 then
  845. s:=s+tostr_with_plus(tai_const(hp).value);
  846. end
  847. else
  848. s:=tostr(tai_const(hp).value);
  849. writer.AsmWrite(s);
  850. inc(l,length(s));
  851. if (l>line_length) or
  852. (hp.next=nil) or
  853. (tai(hp.next).typ<>ait_const) or
  854. (tai_const(hp.next).consttype<>consttype) then
  855. break;
  856. hp:=tai(hp.next);
  857. writer.AsmWrite(',');
  858. until false;
  859. writer.AsmLn;
  860. end;
  861. else
  862. internalerror(200704252);
  863. end;
  864. end;
  865. ait_realconst:
  866. begin
  867. WriteRealConstAsBytes(tai_realconst(hp),#9#9'DB'#9,do_line);
  868. end;
  869. ait_string :
  870. begin
  871. counter := 0;
  872. lines := tai_string(hp).len div line_length;
  873. { separate lines in different parts }
  874. if tai_string(hp).len > 0 then
  875. Begin
  876. for j := 0 to lines-1 do
  877. begin
  878. writer.AsmWrite(#9#9'DB'#9);
  879. quoted:=false;
  880. for i:=counter to counter+line_length-1 do
  881. begin
  882. { it is an ascii character. }
  883. if (ord(tai_string(hp).str[i])>31) and
  884. (ord(tai_string(hp).str[i])<128) and
  885. (tai_string(hp).str[i]<>'"') then
  886. begin
  887. if not(quoted) then
  888. begin
  889. if i>counter then
  890. writer.AsmWrite(',');
  891. writer.AsmWrite('"');
  892. end;
  893. writer.AsmWrite(tai_string(hp).str[i]);
  894. quoted:=true;
  895. end { if > 31 and < 128 and ord('"') }
  896. else
  897. begin
  898. if quoted then
  899. writer.AsmWrite('"');
  900. if i>counter then
  901. writer.AsmWrite(',');
  902. quoted:=false;
  903. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  904. end;
  905. end; { end for i:=0 to... }
  906. if quoted then writer.AsmWrite('"');
  907. writer.AsmWrite(target_info.newline);
  908. inc(counter,line_length);
  909. end; { end for j:=0 ... }
  910. { do last line of lines }
  911. if counter<tai_string(hp).len then
  912. writer.AsmWrite(#9#9'DB'#9);
  913. quoted:=false;
  914. for i:=counter to tai_string(hp).len-1 do
  915. begin
  916. { it is an ascii character. }
  917. if (ord(tai_string(hp).str[i])>31) and
  918. (ord(tai_string(hp).str[i])<128) and
  919. (tai_string(hp).str[i]<>'"') then
  920. begin
  921. if not(quoted) then
  922. begin
  923. if i>counter then
  924. writer.AsmWrite(',');
  925. writer.AsmWrite('"');
  926. end;
  927. writer.AsmWrite(tai_string(hp).str[i]);
  928. quoted:=true;
  929. end { if > 31 and < 128 and " }
  930. else
  931. begin
  932. if quoted then
  933. writer.AsmWrite('"');
  934. if i>counter then
  935. writer.AsmWrite(',');
  936. quoted:=false;
  937. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  938. end;
  939. end; { end for i:=0 to... }
  940. if quoted then
  941. writer.AsmWrite('"');
  942. end;
  943. writer.AsmLn;
  944. end;
  945. ait_label :
  946. begin
  947. if tai_label(hp).labsym.is_used then
  948. begin
  949. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  950. begin
  951. writer.AsmWrite(#9'GLOBAL ');
  952. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name));
  953. end;
  954. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name)+':');
  955. end;
  956. if SmartAsm then
  957. AddSymbol(tai_label(hp).labsym.name,true);
  958. end;
  959. ait_symbol :
  960. begin
  961. if tai_symbol(hp).has_value then
  962. internalerror(2009090803);
  963. if tai_symbol(hp).is_global or SmartAsm then
  964. begin
  965. writer.AsmWrite(#9'GLOBAL ');
  966. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  967. if tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN then
  968. WriteHiddenSymbolAttribute(tai_symbol(hp).sym);
  969. writer.AsmLn;
  970. end;
  971. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  972. if SmartAsm then
  973. AddSymbol(tai_symbol(hp).sym.name,true);
  974. if (not assigned(hp.next)) or (assigned(hp.next) and not(tai(hp.next).typ in
  975. [ait_const,ait_realconst,ait_string])) then
  976. writer.AsmWriteLn(':')
  977. end;
  978. ait_symbol_end : ;
  979. ait_instruction :
  980. begin
  981. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  982. { We need intel order, no At&t }
  983. taicpu(hp).SetOperandOrder(op_intel);
  984. { LOCK must be on same line as opcode }
  985. if (taicpu(hp).ops = 0) and
  986. (fixed_opcode = A_LOCK) then
  987. SkipNewLine:=true
  988. else
  989. SkipNewLine:=false;
  990. s:='';
  991. if ((fixed_opcode=A_FADDP) or
  992. (fixed_opcode=A_FMULP))
  993. and (taicpu(hp).ops=0) then
  994. begin
  995. taicpu(hp).allocate_oper(2);
  996. taicpu(hp).oper[0]^.typ:=top_reg;
  997. taicpu(hp).oper[0]^.reg:=NR_ST1;
  998. taicpu(hp).oper[1]^.typ:=top_reg;
  999. taicpu(hp).oper[1]^.reg:=NR_ST;
  1000. end;
  1001. { NASM only accepts move for loading of
  1002. simple symbol address }
  1003. if ((taicpu(hp).opcode=A_LEA) and
  1004. (taicpu(hp).ops=2) and
  1005. (taicpu(hp).oper[0]^.typ=top_reg) and
  1006. (reg2opsize(taicpu(hp).oper[0]^.reg) in [S_NO,S_Q]) and
  1007. (taicpu(hp).oper[1]^.typ=top_ref) and
  1008. (taicpu(hp).oper[1]^.ref^.refaddr<>addr_no) and
  1009. assigned(taicpu(hp).oper[1]^.ref^.symbol) and
  1010. (taicpu(hp).oper[1]^.ref^.base=NR_NO)) then
  1011. begin
  1012. writer.AsmWrite(asminfo^.comment);
  1013. writer.AsmWriteln('Converting LEA to MOV instruction');
  1014. taicpu(hp).opcode:=A_MOV;
  1015. end;
  1016. if fixed_opcode=A_FWAIT then
  1017. writer.AsmWriteln(#9#9'DB'#9'09bh')
  1018. else if (fixed_opcode=A_XLAT) and (taicpu(hp).ops=1) and
  1019. (taicpu(hp).oper[0]^.typ=top_ref) then
  1020. begin
  1021. writer.AsmWrite(#9#9);
  1022. if (taicpu(hp).oper[0]^.ref^.segment<>NR_NO) and
  1023. (taicpu(hp).oper[0]^.ref^.segment<>NR_DS) then
  1024. writer.AsmWrite(std_regname(taicpu(hp).oper[0]^.ref^.segment)+' ');
  1025. case get_ref_address_size(taicpu(hp).oper[0]^.ref^) of
  1026. 16:
  1027. writer.AsmWrite('a16 ');
  1028. 32:
  1029. writer.AsmWrite('a32 ');
  1030. 64:
  1031. writer.AsmWrite('a64 ');
  1032. end;
  1033. writer.AsmWriteLn('xlatb');
  1034. end
  1035. else if is_x86_parameterized_string_op(fixed_opcode) then
  1036. begin
  1037. writer.AsmWrite(#9#9);
  1038. i:=get_x86_string_op_si_param(fixed_opcode);
  1039. if (i<>-1) and (taicpu(hp).oper[i]^.typ=top_ref) and
  1040. (taicpu(hp).oper[i]^.ref^.segment<>NR_NO) and
  1041. (taicpu(hp).oper[i]^.ref^.segment<>NR_DS) then
  1042. writer.AsmWrite(std_regname(taicpu(hp).oper[i]^.ref^.segment)+' ');
  1043. for i:=0 to taicpu(hp).ops-1 do
  1044. if taicpu(hp).oper[i]^.typ=top_ref then
  1045. begin
  1046. case get_ref_address_size(taicpu(hp).oper[i]^.ref^) of
  1047. 16:
  1048. writer.AsmWrite('a16 ');
  1049. 32:
  1050. writer.AsmWrite('a32 ');
  1051. 64:
  1052. writer.AsmWrite('a64 ');
  1053. end;
  1054. break;
  1055. end;
  1056. writer.AsmWrite(std_op2str[fixed_opcode]);
  1057. case taicpu(hp).opsize of
  1058. S_B:
  1059. writer.AsmWrite('b');
  1060. S_W:
  1061. writer.AsmWrite('w');
  1062. S_L:
  1063. writer.AsmWrite('d');
  1064. S_Q:
  1065. writer.AsmWrite('q');
  1066. else
  1067. internalerror(2017101101);
  1068. end;
  1069. writer.AsmLn;
  1070. end
  1071. else
  1072. begin
  1073. prefix:='';
  1074. {$ifndef i8086}
  1075. { We need to explicitely set
  1076. word prefix to get selectors
  1077. to be pushed in 2 bytes PM }
  1078. if (taicpu(hp).opsize=S_W) and
  1079. ((fixed_opcode=A_PUSH) or
  1080. (fixed_opcode=A_POP)) and
  1081. (taicpu(hp).oper[0]^.typ=top_reg) and
  1082. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  1083. writer.AsmWriteln(#9#9'DB'#9'066h');
  1084. {$endif not i8086}
  1085. if (fixed_opcode=A_RETW) or (fixed_opcode=A_RETNW) or (fixed_opcode=A_RETFW) or
  1086. {$ifdef x86_64}
  1087. (fixed_opcode=A_RETQ) or (fixed_opcode=A_RETNQ) or (fixed_opcode=A_RETFQ) or
  1088. {$else x86_64}
  1089. (fixed_opcode=A_RETD) or (fixed_opcode=A_RETND) or
  1090. {$endif x86_64}
  1091. (fixed_opcode=A_RETFD) then
  1092. begin
  1093. case fixed_opcode of
  1094. A_RETW:
  1095. writer.AsmWrite(#9#9'o16 ret');
  1096. A_RETNW:
  1097. writer.AsmWrite(#9#9'o16 retn');
  1098. A_RETFW:
  1099. writer.AsmWrite(#9#9'o16 retf');
  1100. {$ifdef x86_64}
  1101. A_RETQ,
  1102. A_RETNQ:
  1103. writer.AsmWrite(#9#9'ret');
  1104. A_RETFQ:
  1105. writer.AsmWrite(#9#9'o64 retf');
  1106. {$else x86_64}
  1107. A_RETD:
  1108. writer.AsmWrite(#9#9'o32 ret');
  1109. A_RETND:
  1110. writer.AsmWrite(#9#9'o32 retn');
  1111. {$endif x86_64}
  1112. A_RETFD:
  1113. writer.AsmWrite(#9#9'o32 retf');
  1114. else
  1115. internalerror(2017111001);
  1116. end;
  1117. end
  1118. else if (fixed_opcode=A_SEGCS) or (fixed_opcode=A_SEGDS) or
  1119. (fixed_opcode=A_SEGSS) or (fixed_opcode=A_SEGES) or
  1120. (fixed_opcode=A_SEGFS) or (fixed_opcode=A_SEGGS) then
  1121. begin
  1122. case fixed_opcode of
  1123. A_SEGCS:
  1124. writer.AsmWrite(#9#9'cs');
  1125. A_SEGDS:
  1126. writer.AsmWrite(#9#9'ds');
  1127. A_SEGSS:
  1128. writer.AsmWrite(#9#9'ss');
  1129. A_SEGES:
  1130. writer.AsmWrite(#9#9'es');
  1131. A_SEGFS:
  1132. writer.AsmWrite(#9#9'fs');
  1133. A_SEGGS:
  1134. writer.AsmWrite(#9#9'gs');
  1135. else
  1136. internalerror(2018020101);
  1137. end;
  1138. end
  1139. else
  1140. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  1141. if taicpu(hp).ops<>0 then
  1142. begin
  1143. if is_calljmp(fixed_opcode) then
  1144. begin
  1145. writer.AsmWrite(#9);
  1146. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp));
  1147. end
  1148. else
  1149. begin
  1150. for i:=0 to taicpu(hp).ops-1 do
  1151. begin
  1152. if i=0 then
  1153. writer.AsmWrite(#9)
  1154. else
  1155. writer.AsmWrite(',');
  1156. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  1157. end;
  1158. end;
  1159. end;
  1160. if not SkipNewLine then
  1161. writer.AsmLn;
  1162. end;
  1163. end;
  1164. ait_stab,
  1165. ait_force_line,
  1166. ait_function_name : ;
  1167. ait_cutobject :
  1168. begin
  1169. if SmartAsm then
  1170. begin
  1171. { only reset buffer if nothing has changed }
  1172. if not writer.ClearIfEmpty then
  1173. begin
  1174. if SmartAsm then
  1175. begin
  1176. WriteSmartExternals;
  1177. FreeExternChainList;
  1178. end;
  1179. WriteGroups;
  1180. writer.AsmClose;
  1181. DoAssemble;
  1182. writer.AsmCreate(tai_cutobject(hp).place);
  1183. ResetSectionsList;
  1184. WriteHeader;
  1185. end;
  1186. { avoid empty files }
  1187. LastSecType:=sec_none;
  1188. LastSecName:='';
  1189. LastAlign:=4;
  1190. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1191. begin
  1192. if tai(hp.next).typ=ait_section then
  1193. begin
  1194. LastSecType:=tai_section(hp.next).sectype;
  1195. LastSecName:=tai_section(hp.next).name^;
  1196. LastAlign:=tai_section(hp.next).secalign;
  1197. end;
  1198. hp:=tai(hp.next);
  1199. end;
  1200. if LastSecType<>sec_none then
  1201. WriteSection(LastSecType,LastSecName,LastAlign);
  1202. writer.MarkEmpty;
  1203. NewObject:=true;
  1204. end;
  1205. end;
  1206. ait_marker :
  1207. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1208. inc(InlineLevel)
  1209. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1210. dec(InlineLevel);
  1211. ait_directive :
  1212. begin
  1213. case tai_directive(hp).directive of
  1214. asd_nasm_import,
  1215. asd_extern :
  1216. begin
  1217. case tai_directive(hp).directive of
  1218. asd_nasm_import :
  1219. writer.AsmWrite('import ');
  1220. asd_extern :
  1221. writer.AsmWrite('EXTERN ');
  1222. else
  1223. internalerror(200509191);
  1224. end;
  1225. if tai_directive(hp).name<>'' then
  1226. begin
  1227. if SmartAsm then
  1228. AddSymbol(tai_directive(hp).name,false);
  1229. writer.AsmWrite(tai_directive(hp).name);
  1230. end;
  1231. end;
  1232. asd_cpu :
  1233. begin
  1234. writer.AsmWrite('CPU ');
  1235. for cpu:=low(tcputype) to high(tcputype) do
  1236. begin
  1237. if tai_directive(hp).name=CPUTypeStr[CPU] then
  1238. begin
  1239. writer.AsmWriteLn(nasm_cpu_name[cpu]);
  1240. break;
  1241. end;
  1242. end;
  1243. end;
  1244. {$ifdef OMFOBJSUPPORT}
  1245. asd_omf_linnum_line :
  1246. writer.AsmWriteLn('; OMF LINNUM Line '+tai_directive(hp).name);
  1247. {$endif OMFOBJSUPPORT}
  1248. else
  1249. internalerror(2005091903);
  1250. end;
  1251. writer.AsmLn;
  1252. end;
  1253. ait_seh_directive :
  1254. { Ignore for now };
  1255. else
  1256. if not WriteComments(hp) then
  1257. internalerror(2020100801);
  1258. end;
  1259. hp:=tai(hp.next);
  1260. end;
  1261. end;
  1262. procedure TX86NasmAssembler.WriteExternals;
  1263. var
  1264. sym : TAsmSymbol;
  1265. i : longint;
  1266. begin
  1267. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  1268. begin
  1269. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1270. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  1271. writer.AsmWriteln('EXTERN'#9+ApplyAsmSymbolRestrictions(sym.name));
  1272. end;
  1273. end;
  1274. procedure TX86NasmAssembler.WriteSmartExternals;
  1275. var
  1276. EC : PExternChain;
  1277. begin
  1278. EC:=FEC;
  1279. while assigned(EC) do
  1280. begin
  1281. if not EC^.is_defined then
  1282. writer.AsmWriteln('EXTERN'#9+ApplyAsmSymbolRestrictions(EC^.psym^));
  1283. EC:=EC^.next;
  1284. end;
  1285. end;
  1286. procedure TX86NasmAssembler.WriteHeader;
  1287. begin
  1288. {$if defined(i8086)}
  1289. writer.AsmWriteLn('BITS 16');
  1290. {$elseif defined(i386)}
  1291. writer.AsmWriteLn('BITS 32');
  1292. using_relative:=false;
  1293. {$elseif defined(x86_64)}
  1294. writer.AsmWriteLn('BITS 64');
  1295. writer.AsmWriteLn('default rel');
  1296. using_relative:=true;
  1297. {$endif}
  1298. writer.AsmWriteLn('CPU '+nasm_cpu_name[current_settings.cputype]);
  1299. end;
  1300. procedure TX86NasmAssembler.WriteAsmList;
  1301. var
  1302. hal : tasmlisttype;
  1303. begin
  1304. {$ifdef EXTDEBUG}
  1305. if current_module.mainsource<>'' then
  1306. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1307. {$endif}
  1308. ResetSectionsList;
  1309. WriteHeader;
  1310. writer.AsmLn;
  1311. WriteExternals;
  1312. for hal:=low(TasmlistType) to high(TasmlistType) do
  1313. begin
  1314. if not (current_asmdata.asmlists[hal].empty) then
  1315. begin
  1316. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1317. writetree(current_asmdata.asmlists[hal]);
  1318. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1319. end;
  1320. end;
  1321. writer.AsmLn;
  1322. if SmartAsm then
  1323. begin
  1324. WriteSmartExternals;
  1325. FreeExternChainList;
  1326. end;
  1327. WriteGroups;
  1328. {$ifdef EXTDEBUG}
  1329. if current_module.mainsource<>'' then
  1330. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1331. {$endif EXTDEBUG}
  1332. end;
  1333. function TX86NasmAssembler.MakeCmdLine: TCmdStr;
  1334. var
  1335. FormatName : string;
  1336. begin
  1337. result:=Inherited MakeCmdLine;
  1338. {$ifdef i8086}
  1339. case target_info.system of
  1340. system_i8086_msdos,
  1341. system_i8086_win16,
  1342. system_i8086_embedded:
  1343. begin
  1344. FormatName:='obj';
  1345. if (cs_debuginfo in current_settings.moduleswitches) or
  1346. (cs_asm_source in current_settings.globalswitches) then
  1347. Replace(result,'$DEBUG','-g')
  1348. else
  1349. Replace(result,'$DEBUG','');
  1350. end
  1351. else
  1352. internalerror(2014082060);
  1353. end;
  1354. {$endif i8086}
  1355. {$ifdef i386}
  1356. case target_info.system of
  1357. system_i386_go32v2:
  1358. FormatName:='coff';
  1359. system_i386_wdosx,
  1360. system_i386_win32:
  1361. FormatName:='win32';
  1362. system_i386_embedded:
  1363. FormatName:='obj';
  1364. system_i386_linux,
  1365. system_i386_beos:
  1366. FormatName:='elf';
  1367. system_i386_darwin:
  1368. FormatName:='macho32';
  1369. else
  1370. FormatName:='elf';
  1371. end;
  1372. {$endif i386}
  1373. {$ifdef x86_64}
  1374. case target_info.system of
  1375. system_x86_64_win64:
  1376. FormatName:='win64';
  1377. system_x86_64_darwin:
  1378. FormatName:='macho64';
  1379. system_x86_64_embedded:
  1380. FormatName:='obj';
  1381. system_x86_64_linux:
  1382. FormatName:='elf64';
  1383. else
  1384. FormatName:='elf64';
  1385. end;
  1386. {$endif x86_64}
  1387. Replace(result,'$FORMAT',FormatName);
  1388. end;
  1389. {*****************************************************************************
  1390. Initialize
  1391. *****************************************************************************}
  1392. {$ifdef i8086}
  1393. const
  1394. as_i8086_nasm_info : tasminfo =
  1395. (
  1396. id : as_i8086_nasm;
  1397. idtxt : 'NASM';
  1398. asmbin : 'nasm';
  1399. asmcmd : '-f $FORMAT $DEBUG -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1400. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1401. flags : [af_needar,af_no_debug];
  1402. labelprefix : '..@';
  1403. labelmaxlen : -1;
  1404. comment : '; ';
  1405. dollarsign: '$';
  1406. );
  1407. as_i8086_nasmobj_info : tasminfo =
  1408. (
  1409. id : as_i8086_nasmobj;
  1410. idtxt : 'NASMOBJ';
  1411. asmbin : 'nasm';
  1412. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1413. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1414. flags : [af_needar,af_no_debug];
  1415. labelprefix : '..@';
  1416. labelmaxlen : -1;
  1417. comment : '; ';
  1418. dollarsign: '$';
  1419. );
  1420. {$endif i8086}
  1421. {$ifdef i386}
  1422. const
  1423. as_i386_nasmcoff_info : tasminfo =
  1424. (
  1425. id : as_i386_nasmcoff;
  1426. idtxt : 'NASMCOFF';
  1427. asmbin : 'nasm';
  1428. asmcmd : '-f coff -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1429. supported_targets : [system_i386_go32v2];
  1430. flags : [af_needar,af_no_debug];
  1431. labelprefix : '..@';
  1432. labelmaxlen : -1;
  1433. comment : '; ';
  1434. dollarsign: '$';
  1435. );
  1436. as_i386_nasmwin32_info : tasminfo =
  1437. (
  1438. id : as_i386_nasmwin32;
  1439. idtxt : 'NASMWIN32';
  1440. asmbin : 'nasm';
  1441. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1442. supported_targets : [system_i386_win32];
  1443. flags : [af_needar,af_no_debug,af_smartlink_sections];
  1444. labelprefix : '..@';
  1445. labelmaxlen : -1;
  1446. comment : '; ';
  1447. dollarsign: '$';
  1448. );
  1449. as_i386_nasmobj_info : tasminfo =
  1450. (
  1451. id : as_i386_nasmobj;
  1452. idtxt : 'NASMOBJ';
  1453. asmbin : 'nasm';
  1454. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1455. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1456. flags : [af_needar,af_no_debug];
  1457. labelprefix : '..@';
  1458. labelmaxlen : -1;
  1459. comment : '; ';
  1460. dollarsign: '$';
  1461. );
  1462. as_i386_nasmwdosx_info : tasminfo =
  1463. (
  1464. id : as_i386_nasmwdosx;
  1465. idtxt : 'NASMWDOSX';
  1466. asmbin : 'nasm';
  1467. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1468. supported_targets : [system_i386_wdosx];
  1469. flags : [af_needar,af_no_debug];
  1470. labelprefix : '..@';
  1471. labelmaxlen : -1;
  1472. comment : '; ';
  1473. dollarsign: '$';
  1474. );
  1475. as_i386_nasmelf_info : tasminfo =
  1476. (
  1477. id : as_i386_nasmelf;
  1478. idtxt : 'NASMELF';
  1479. asmbin : 'nasm';
  1480. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1481. supported_targets : [system_i386_linux];
  1482. flags : [af_needar,af_no_debug];
  1483. labelprefix : '..@';
  1484. labelmaxlen : -1;
  1485. comment : '; ';
  1486. dollarsign: '$';
  1487. );
  1488. {
  1489. as_i386_nasmdarwin_info : tasminfo =
  1490. (
  1491. id : as_i386_nasmdarwin;
  1492. idtxt : 'NASMDARWIN';
  1493. asmbin : 'nasm';
  1494. asmcmd : '-f macho32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1495. supported_targets : [system_i386_darwin];
  1496. flags : [af_needar,af_no_debug];
  1497. labelprefix : '..@';
  1498. labelmaxlen : -1;
  1499. comment : '; ';
  1500. dollarsign: '$';
  1501. );
  1502. }
  1503. as_i386_nasmbeos_info : tasminfo =
  1504. (
  1505. id : as_i386_nasmbeos;
  1506. idtxt : 'NASMELF';
  1507. asmbin : 'nasm';
  1508. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1509. supported_targets : [system_i386_beos];
  1510. flags : [af_needar,af_no_debug];
  1511. labelprefix : '..@';
  1512. labelmaxlen : -1;
  1513. comment : '; ';
  1514. dollarsign: '$';
  1515. );
  1516. as_i386_nasmhaiku_info : tasminfo =
  1517. (
  1518. id : as_i386_nasmhaiku;
  1519. idtxt : 'NASMELF';
  1520. asmbin : 'nasm';
  1521. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1522. supported_targets : [system_i386_haiku];
  1523. flags : [af_needar,af_no_debug];
  1524. labelprefix : '..@';
  1525. labelmaxlen : -1;
  1526. comment : '; ';
  1527. dollarsign: '$';
  1528. );
  1529. as_i386_nasm_info : tasminfo =
  1530. (
  1531. id : as_i386_nasm;
  1532. idtxt : 'NASM';
  1533. asmbin : 'nasm';
  1534. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1535. supported_targets : [system_any];
  1536. flags : [af_needar,af_no_debug];
  1537. labelprefix : '..@';
  1538. labelmaxlen : -1;
  1539. comment : '; ';
  1540. dollarsign: '$';
  1541. );
  1542. {$endif i386}
  1543. {$ifdef x86_64}
  1544. const
  1545. as_x86_64_nasm_info : tasminfo =
  1546. (
  1547. id : as_x86_64_nasm;
  1548. idtxt : 'NASM';
  1549. asmbin : 'nasm';
  1550. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1551. supported_targets : [system_any];
  1552. flags : [af_needar{,af_no_debug}];
  1553. labelprefix : '..@';
  1554. labelmaxlen : -1;
  1555. comment : '; ';
  1556. dollarsign: '$';
  1557. );
  1558. as_x86_64_nasmwin64_info : tasminfo =
  1559. (
  1560. id : as_x86_64_nasmwin64;
  1561. idtxt : 'NASMWIN64';
  1562. asmbin : 'nasm';
  1563. asmcmd : '-f win64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1564. supported_targets : [system_x86_64_win64];
  1565. flags : [af_needar,af_no_debug];
  1566. labelprefix : '..@';
  1567. labelmaxlen : -1;
  1568. comment : '; ';
  1569. dollarsign: '$';
  1570. );
  1571. as_x86_64_nasmelf_info : tasminfo =
  1572. (
  1573. id : as_x86_64_nasmelf;
  1574. idtxt : 'NASMELF';
  1575. asmbin : 'nasm';
  1576. asmcmd : '-f elf64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1577. supported_targets : [system_x86_64_linux];
  1578. flags : [af_needar,af_no_debug];
  1579. labelprefix : '..@';
  1580. labelmaxlen : -1;
  1581. comment : '; ';
  1582. dollarsign: '$';
  1583. );
  1584. as_x86_64_nasmdarwin_info : tasminfo =
  1585. (
  1586. id : as_x86_64_nasmdarwin;
  1587. idtxt : 'NASMDARWIN';
  1588. asmbin : 'nasm';
  1589. asmcmd : '-f macho64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1590. supported_targets : [system_x86_64_darwin];
  1591. flags : [af_needar,af_no_debug];
  1592. labelprefix : '..@';
  1593. labelmaxlen : -1;
  1594. comment : '; ';
  1595. dollarsign: '$';
  1596. );
  1597. {$endif x86_64}
  1598. initialization
  1599. {$ifdef i8086}
  1600. RegisterAssembler(as_i8086_nasm_info,TX86NasmAssembler);
  1601. RegisterAssembler(as_i8086_nasmobj_info,TX86NasmAssembler);
  1602. {$endif i8086}
  1603. {$ifdef i386}
  1604. RegisterAssembler(as_i386_nasmcoff_info,TX86NasmAssembler);
  1605. RegisterAssembler(as_i386_nasmwin32_info,TX86NasmAssembler);
  1606. RegisterAssembler(as_i386_nasmwdosx_info,TX86NasmAssembler);
  1607. RegisterAssembler(as_i386_nasmobj_info,TX86NasmAssembler);
  1608. RegisterAssembler(as_i386_nasmbeos_info,TX86NasmAssembler);
  1609. RegisterAssembler(as_i386_nasmhaiku_info,TX86NasmAssembler);
  1610. RegisterAssembler(as_i386_nasmelf_info,TX86NasmAssembler);
  1611. RegisterAssembler(as_i386_nasm_info,TX86NasmAssembler);
  1612. {$endif i386}
  1613. {$ifdef x86_64}
  1614. RegisterAssembler(as_x86_64_nasm_info,TX86NasmAssembler);
  1615. RegisterAssembler(as_x86_64_nasmwin64_info,TX86NasmAssembler);
  1616. RegisterAssembler(as_x86_64_nasmelf_info,TX86NasmAssembler);
  1617. RegisterAssembler(as_x86_64_nasmdarwin_info,TX86NasmAssembler);
  1618. {$endif x86_64}
  1619. end.