agx86nsm.pas 60 KB

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