agx86nsm.pas 59 KB

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