agx86nsm.pas 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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] 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] 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. FormatName:='obj';
  1173. else
  1174. internalerror(2014082060);
  1175. end;
  1176. {$endif i8086}
  1177. {$ifdef i386}
  1178. case target_info.system of
  1179. system_i386_go32v2:
  1180. FormatName:='coff';
  1181. system_i386_wdosx,
  1182. system_i386_win32:
  1183. FormatName:='win32';
  1184. system_i386_embedded:
  1185. FormatName:='obj';
  1186. system_i386_linux,
  1187. system_i386_beos:
  1188. FormatName:='elf';
  1189. system_i386_darwin:
  1190. FormatName:='macho32';
  1191. else
  1192. FormatName:='elf';
  1193. end;
  1194. {$endif i386}
  1195. {$ifdef x86_64}
  1196. case target_info.system of
  1197. system_x86_64_win64:
  1198. FormatName:='win64';
  1199. system_x86_64_darwin:
  1200. FormatName:='macho64';
  1201. system_x86_64_embedded:
  1202. FormatName:='obj';
  1203. system_x86_64_linux:
  1204. FormatName:='elf64';
  1205. else
  1206. FormatName:='elf64';
  1207. end;
  1208. {$endif x86_64}
  1209. Replace(result,'$FORMAT',FormatName);
  1210. end;
  1211. {*****************************************************************************
  1212. Initialize
  1213. *****************************************************************************}
  1214. {$ifdef i8086}
  1215. const
  1216. as_i8086_nasm_info : tasminfo =
  1217. (
  1218. id : as_i8086_nasm;
  1219. idtxt : 'NASM';
  1220. asmbin : 'nasm';
  1221. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1222. supported_targets : [system_i8086_msdos,system_i8086_win16];
  1223. flags : [af_needar,af_no_debug];
  1224. labelprefix : '..@';
  1225. comment : '; ';
  1226. dollarsign: '$';
  1227. );
  1228. as_i8086_nasmobj_info : tasminfo =
  1229. (
  1230. id : as_i8086_nasmobj;
  1231. idtxt : 'NASMOBJ';
  1232. asmbin : 'nasm';
  1233. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1234. supported_targets : [system_i8086_msdos,system_i8086_win16];
  1235. flags : [af_needar,af_no_debug];
  1236. labelprefix : '..@';
  1237. comment : '; ';
  1238. dollarsign: '$';
  1239. );
  1240. {$endif i8086}
  1241. {$ifdef i386}
  1242. const
  1243. as_i386_nasmcoff_info : tasminfo =
  1244. (
  1245. id : as_i386_nasmcoff;
  1246. idtxt : 'NASMCOFF';
  1247. asmbin : 'nasm';
  1248. asmcmd : '-f coff -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1249. supported_targets : [system_i386_go32v2];
  1250. flags : [af_needar,af_no_debug];
  1251. labelprefix : '..@';
  1252. comment : '; ';
  1253. dollarsign: '$';
  1254. );
  1255. as_i386_nasmwin32_info : tasminfo =
  1256. (
  1257. id : as_i386_nasmwin32;
  1258. idtxt : 'NASMWIN32';
  1259. asmbin : 'nasm';
  1260. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1261. supported_targets : [system_i386_win32];
  1262. flags : [af_needar,af_no_debug,af_smartlink_sections];
  1263. labelprefix : '..@';
  1264. comment : '; ';
  1265. dollarsign: '$';
  1266. );
  1267. as_i386_nasmobj_info : tasminfo =
  1268. (
  1269. id : as_i386_nasmobj;
  1270. idtxt : 'NASMOBJ';
  1271. asmbin : 'nasm';
  1272. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1273. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1274. flags : [af_needar,af_no_debug];
  1275. labelprefix : '..@';
  1276. comment : '; ';
  1277. dollarsign: '$';
  1278. );
  1279. as_i386_nasmwdosx_info : tasminfo =
  1280. (
  1281. id : as_i386_nasmwdosx;
  1282. idtxt : 'NASMWDOSX';
  1283. asmbin : 'nasm';
  1284. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1285. supported_targets : [system_i386_wdosx];
  1286. flags : [af_needar,af_no_debug];
  1287. labelprefix : '..@';
  1288. comment : '; ';
  1289. dollarsign: '$';
  1290. );
  1291. as_i386_nasmelf_info : tasminfo =
  1292. (
  1293. id : as_i386_nasmelf;
  1294. idtxt : 'NASMELF';
  1295. asmbin : 'nasm';
  1296. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1297. supported_targets : [system_i386_linux];
  1298. flags : [af_needar,af_no_debug];
  1299. labelprefix : '..@';
  1300. comment : '; ';
  1301. dollarsign: '$';
  1302. );
  1303. {
  1304. as_i386_nasmdarwin_info : tasminfo =
  1305. (
  1306. id : as_i386_nasmdarwin;
  1307. idtxt : 'NASMDARWIN';
  1308. asmbin : 'nasm';
  1309. asmcmd : '-f macho32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1310. supported_targets : [system_i386_darwin];
  1311. flags : [af_needar,af_no_debug];
  1312. labelprefix : '..@';
  1313. comment : '; ';
  1314. dollarsign: '$';
  1315. );
  1316. }
  1317. as_i386_nasmbeos_info : tasminfo =
  1318. (
  1319. id : as_i386_nasmbeos;
  1320. idtxt : 'NASMELF';
  1321. asmbin : 'nasm';
  1322. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1323. supported_targets : [system_i386_beos];
  1324. flags : [af_needar,af_no_debug];
  1325. labelprefix : '..@';
  1326. comment : '; ';
  1327. dollarsign: '$';
  1328. );
  1329. as_i386_nasmhaiku_info : tasminfo =
  1330. (
  1331. id : as_i386_nasmhaiku;
  1332. idtxt : 'NASMELF';
  1333. asmbin : 'nasm';
  1334. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1335. supported_targets : [system_i386_haiku];
  1336. flags : [af_needar,af_no_debug];
  1337. labelprefix : '..@';
  1338. comment : '; ';
  1339. dollarsign: '$';
  1340. );
  1341. as_i386_nasm_info : tasminfo =
  1342. (
  1343. id : as_i386_nasm;
  1344. idtxt : 'NASM';
  1345. asmbin : 'nasm';
  1346. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1347. supported_targets : [system_any];
  1348. flags : [af_needar,af_no_debug];
  1349. labelprefix : '..@';
  1350. comment : '; ';
  1351. dollarsign: '$';
  1352. );
  1353. {$endif i386}
  1354. {$ifdef x86_64}
  1355. const
  1356. as_x86_64_nasm_info : tasminfo =
  1357. (
  1358. id : as_x86_64_nasm;
  1359. idtxt : 'NASM';
  1360. asmbin : 'nasm';
  1361. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1362. supported_targets : [system_any];
  1363. flags : [af_needar{,af_no_debug}];
  1364. labelprefix : '..@';
  1365. comment : '; ';
  1366. dollarsign: '$';
  1367. );
  1368. as_x86_64_nasmwin64_info : tasminfo =
  1369. (
  1370. id : as_x86_64_nasmwin64;
  1371. idtxt : 'NASMWIN64';
  1372. asmbin : 'nasm';
  1373. asmcmd : '-f win64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1374. supported_targets : [system_x86_64_win64];
  1375. flags : [af_needar,af_no_debug];
  1376. labelprefix : '..@';
  1377. comment : '; ';
  1378. dollarsign: '$';
  1379. );
  1380. as_x86_64_nasmelf_info : tasminfo =
  1381. (
  1382. id : as_x86_64_nasmelf;
  1383. idtxt : 'NASMELF';
  1384. asmbin : 'nasm';
  1385. asmcmd : '-f elf64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1386. supported_targets : [system_x86_64_linux];
  1387. flags : [af_needar,af_no_debug];
  1388. labelprefix : '..@';
  1389. comment : '; ';
  1390. dollarsign: '$';
  1391. );
  1392. as_x86_64_nasmdarwin_info : tasminfo =
  1393. (
  1394. id : as_x86_64_nasmdarwin;
  1395. idtxt : 'NASMDARWIN';
  1396. asmbin : 'nasm';
  1397. asmcmd : '-f macho64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1398. supported_targets : [system_x86_64_darwin];
  1399. flags : [af_needar,af_no_debug];
  1400. labelprefix : '..@';
  1401. comment : '; ';
  1402. dollarsign: '$';
  1403. );
  1404. {$endif x86_64}
  1405. initialization
  1406. {$ifdef i8086}
  1407. RegisterAssembler(as_i8086_nasm_info,TX86NasmAssembler);
  1408. RegisterAssembler(as_i8086_nasmobj_info,TX86NasmAssembler);
  1409. {$endif i8086}
  1410. {$ifdef i386}
  1411. RegisterAssembler(as_i386_nasmcoff_info,TX86NasmAssembler);
  1412. RegisterAssembler(as_i386_nasmwin32_info,TX86NasmAssembler);
  1413. RegisterAssembler(as_i386_nasmwdosx_info,TX86NasmAssembler);
  1414. RegisterAssembler(as_i386_nasmobj_info,TX86NasmAssembler);
  1415. RegisterAssembler(as_i386_nasmbeos_info,TX86NasmAssembler);
  1416. RegisterAssembler(as_i386_nasmhaiku_info,TX86NasmAssembler);
  1417. RegisterAssembler(as_i386_nasmelf_info,TX86NasmAssembler);
  1418. RegisterAssembler(as_i386_nasm_info,TX86NasmAssembler);
  1419. {$endif i386}
  1420. {$ifdef x86_64}
  1421. RegisterAssembler(as_x86_64_nasm_info,TX86NasmAssembler);
  1422. RegisterAssembler(as_x86_64_nasmwin64_info,TX86NasmAssembler);
  1423. RegisterAssembler(as_x86_64_nasmelf_info,TX86NasmAssembler);
  1424. RegisterAssembler(as_x86_64_nasmdarwin_info,TX86NasmAssembler);
  1425. {$endif x86_64}
  1426. end.