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