agx86nsm.pas 56 KB

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