agx86nsm.pas 60 KB

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