agx86nsm.pas 53 KB

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