agx86nsm.pas 59 KB

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