agx86nsm.pas 59 KB

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