agx86nsm.pas 60 KB

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