agx86nsm.pas 60 KB

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