agx86nsm.pas 51 KB

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