agx86nsm.pas 60 KB

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