agx86nsm.pas 52 KB

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