agx86nsm.pas 59 KB

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