agx86nsm.pas 59 KB

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