2
0

agx86nsm.pas 60 KB

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