agx86nsm.pas 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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. begin
  591. if not assigned(p) then
  592. exit;
  593. InlineLevel:=0;
  594. NewObject:=true;
  595. { lineinfo is only needed for al_procedures (PFV) }
  596. do_line:=(cs_asm_source in current_settings.globalswitches) or
  597. ((cs_lineinfo in current_settings.moduleswitches)
  598. and (p=current_asmdata.asmlists[al_procedures]));
  599. hp:=tai(p.first);
  600. while assigned(hp) do
  601. begin
  602. prefetch(pointer(hp.next)^);
  603. if not(hp.typ in SkipLineInfo) then
  604. begin
  605. previnfile:=lastinfile;
  606. prevfileinfo:=lastfileinfo;
  607. current_filepos:=tailineinfo(hp).fileinfo;
  608. { no line info for inlined code }
  609. if do_line and (inlinelevel=0) then
  610. WriteSourceLine(hp as tailineinfo);
  611. if (lastfileinfo.line<>prevfileinfo.line) or
  612. (previnfile<>lastinfile) then
  613. begin
  614. { +0 postfix means no line increment per assembler instruction }
  615. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  616. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  617. writer.AsmWriteLn(' '+lastinfile.name)
  618. else
  619. writer.AsmLn;
  620. NewObject:=false;
  621. end;
  622. end;
  623. case hp.typ of
  624. ait_comment :
  625. Begin
  626. writer.AsmWrite(asminfo^.comment);
  627. writer.AsmWritePChar(tai_comment(hp).str);
  628. writer.AsmLn;
  629. End;
  630. ait_regalloc :
  631. begin
  632. if (cs_asm_regalloc in current_settings.globalswitches) then
  633. writer.AsmWriteLn(#9#9+asminfo^.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+' '+
  634. regallocstr[tai_regalloc(hp).ratype]);
  635. end;
  636. ait_tempalloc :
  637. begin
  638. if (cs_asm_tempalloc in current_settings.globalswitches) then
  639. WriteTempalloc(tai_tempalloc(hp));
  640. end;
  641. ait_section :
  642. begin
  643. if tai_section(hp).sectype<>sec_none then
  644. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secalign);
  645. LastSecType:=tai_section(hp).sectype;
  646. end;
  647. ait_align :
  648. begin
  649. if (tai_align(hp).aligntype>1) then
  650. begin
  651. if (LastSecType=sec_bss) or (
  652. (LastSecType=sec_threadvar) and
  653. (target_info.system in (systems_windows+systems_wince))
  654. ) then
  655. writer.AsmWriteLn(#9'ALIGNB '+tostr(tai_align(hp).aligntype))
  656. else if tai_align_abstract(hp).use_op then
  657. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB '+tostr(tai_align_abstract(hp).fillop))
  658. else if LastSecType in [sec_code,sec_stub,sec_init,sec_fini] then
  659. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype))
  660. else
  661. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB 0');
  662. end;
  663. end;
  664. ait_datablock :
  665. begin
  666. if tai_datablock(hp).is_global or SmartAsm then
  667. begin
  668. writer.AsmWrite(#9'GLOBAL ');
  669. writer.AsmWriteLn(tai_datablock(hp).sym.name);
  670. end;
  671. writer.AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  672. if SmartAsm then
  673. AddSymbol(tai_datablock(hp).sym.name,true);
  674. writer.AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  675. end;
  676. ait_const:
  677. begin
  678. consttype:=tai_const(hp).consttype;
  679. case consttype of
  680. aitconst_uleb128bit,
  681. aitconst_sleb128bit,
  682. aitconst_128bit:
  683. begin
  684. writer.AsmWriteLn(asminfo^.comment+'Unsupported const type '+
  685. ait_const2str[consttype]);
  686. end;
  687. {$ifdef i8086}
  688. aitconst_farptr:
  689. begin
  690. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  691. if assigned(tai_const(hp).sym) then
  692. begin
  693. if SmartAsm then
  694. AddSymbol(tai_const(hp).sym.name,false);
  695. writer.AsmWrite(tai_const(hp).sym.name);
  696. if tai_const(hp).value<>0 then
  697. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  698. writer.AsmLn;
  699. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  700. writer.AsmWrite('SEG ');
  701. writer.AsmWrite(tai_const(hp).sym.name);
  702. end
  703. else
  704. writer.AsmWrite(tostr(lo(longint(tai_const(hp).value)))+','+
  705. tostr(hi(longint(tai_const(hp).value))));
  706. writer.AsmLn;
  707. end;
  708. aitconst_seg:
  709. begin
  710. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  711. if assigned(tai_const(hp).sym) then
  712. begin
  713. if SmartAsm then
  714. AddSymbol(tai_const(hp).sym.name,false);
  715. writer.AsmWrite('SEG ');
  716. writer.AsmWrite(tai_const(hp).sym.name);
  717. end
  718. else
  719. internalerror(2015110501);
  720. writer.AsmLn;
  721. end;
  722. aitconst_dgroup:
  723. writer.AsmWriteLn(#9'DW'#9'DGROUP');
  724. aitconst_fardataseg:
  725. writer.AsmWriteLn(#9'DW'#9+current_module.modulename^+'_DATA');
  726. {$endif i8086}
  727. aitconst_64bit,
  728. aitconst_32bit,
  729. aitconst_16bit,
  730. aitconst_8bit,
  731. aitconst_rva_symbol,
  732. aitconst_secrel32_symbol,
  733. aitconst_16bit_unaligned,
  734. aitconst_32bit_unaligned,
  735. aitconst_64bit_unaligned:
  736. begin
  737. writer.AsmWrite(ait_const2str[tai_const(hp).consttype]);
  738. l:=0;
  739. repeat
  740. if assigned(tai_const(hp).sym) then
  741. begin
  742. if SmartAsm then
  743. begin
  744. AddSymbol(tai_const(hp).sym.name,false);
  745. if assigned(tai_const(hp).endsym) then
  746. AddSymbol(tai_const(hp).endsym.name,false);
  747. end;
  748. if assigned(tai_const(hp).endsym) then
  749. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  750. else
  751. s:=tai_const(hp).sym.name;
  752. if tai_const(hp).value<>0 then
  753. s:=s+tostr_with_plus(tai_const(hp).value);
  754. end
  755. else
  756. s:=tostr(tai_const(hp).value);
  757. writer.AsmWrite(s);
  758. inc(l,length(s));
  759. if (l>line_length) or
  760. (hp.next=nil) or
  761. (tai(hp.next).typ<>ait_const) or
  762. (tai_const(hp.next).consttype<>consttype) then
  763. break;
  764. hp:=tai(hp.next);
  765. writer.AsmWrite(',');
  766. until false;
  767. writer.AsmLn;
  768. end;
  769. else
  770. internalerror(200704252);
  771. end;
  772. end;
  773. ait_realconst:
  774. begin
  775. WriteRealConstAsBytes(tai_realconst(hp),#9#9'DB'#9,do_line);
  776. end;
  777. ait_string :
  778. begin
  779. counter := 0;
  780. lines := tai_string(hp).len div line_length;
  781. { separate lines in different parts }
  782. if tai_string(hp).len > 0 then
  783. Begin
  784. for j := 0 to lines-1 do
  785. begin
  786. writer.AsmWrite(#9#9'DB'#9);
  787. quoted:=false;
  788. for i:=counter to counter+line_length-1 do
  789. begin
  790. { it is an ascii character. }
  791. if (ord(tai_string(hp).str[i])>31) and
  792. (ord(tai_string(hp).str[i])<128) and
  793. (tai_string(hp).str[i]<>'"') then
  794. begin
  795. if not(quoted) then
  796. begin
  797. if i>counter then
  798. writer.AsmWrite(',');
  799. writer.AsmWrite('"');
  800. end;
  801. writer.AsmWrite(tai_string(hp).str[i]);
  802. quoted:=true;
  803. end { if > 31 and < 128 and ord('"') }
  804. else
  805. begin
  806. if quoted then
  807. writer.AsmWrite('"');
  808. if i>counter then
  809. writer.AsmWrite(',');
  810. quoted:=false;
  811. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  812. end;
  813. end; { end for i:=0 to... }
  814. if quoted then writer.AsmWrite('"');
  815. writer.AsmWrite(target_info.newline);
  816. inc(counter,line_length);
  817. end; { end for j:=0 ... }
  818. { do last line of lines }
  819. if counter<tai_string(hp).len then
  820. writer.AsmWrite(#9#9'DB'#9);
  821. quoted:=false;
  822. for i:=counter to tai_string(hp).len-1 do
  823. begin
  824. { it is an ascii character. }
  825. if (ord(tai_string(hp).str[i])>31) and
  826. (ord(tai_string(hp).str[i])<128) and
  827. (tai_string(hp).str[i]<>'"') then
  828. begin
  829. if not(quoted) then
  830. begin
  831. if i>counter then
  832. writer.AsmWrite(',');
  833. writer.AsmWrite('"');
  834. end;
  835. writer.AsmWrite(tai_string(hp).str[i]);
  836. quoted:=true;
  837. end { if > 31 and < 128 and " }
  838. else
  839. begin
  840. if quoted then
  841. writer.AsmWrite('"');
  842. if i>counter then
  843. writer.AsmWrite(',');
  844. quoted:=false;
  845. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  846. end;
  847. end; { end for i:=0 to... }
  848. if quoted then
  849. writer.AsmWrite('"');
  850. end;
  851. writer.AsmLn;
  852. end;
  853. ait_label :
  854. begin
  855. if tai_label(hp).labsym.is_used then
  856. begin
  857. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  858. begin
  859. writer.AsmWrite(#9'GLOBAL ');
  860. writer.AsmWriteLn(tai_label(hp).labsym.name);
  861. end;
  862. writer.AsmWriteLn(tai_label(hp).labsym.name+':');
  863. end;
  864. if SmartAsm then
  865. AddSymbol(tai_label(hp).labsym.name,true);
  866. end;
  867. ait_symbol :
  868. begin
  869. if tai_symbol(hp).has_value then
  870. internalerror(2009090803);
  871. if tai_symbol(hp).is_global or SmartAsm then
  872. begin
  873. writer.AsmWrite(#9'GLOBAL ');
  874. writer.AsmWriteLn(tai_symbol(hp).sym.name);
  875. end;
  876. writer.AsmWrite(tai_symbol(hp).sym.name);
  877. if SmartAsm then
  878. AddSymbol(tai_symbol(hp).sym.name,true);
  879. if (not assigned(hp.next)) or (assigned(hp.next) and not(tai(hp.next).typ in
  880. [ait_const,ait_realconst,ait_string])) then
  881. writer.AsmWriteLn(':')
  882. end;
  883. ait_symbol_end : ;
  884. ait_instruction :
  885. begin
  886. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  887. { We need intel order, no At&t }
  888. taicpu(hp).SetOperandOrder(op_intel);
  889. { LOCK must be on same line as opcode }
  890. if (taicpu(hp).ops = 0) and
  891. (fixed_opcode = A_LOCK) then
  892. SkipNewLine:=true
  893. else
  894. SkipNewLine:=false;
  895. s:='';
  896. if ((fixed_opcode=A_FADDP) or
  897. (fixed_opcode=A_FMULP))
  898. and (taicpu(hp).ops=0) then
  899. begin
  900. taicpu(hp).allocate_oper(2);
  901. taicpu(hp).oper[0]^.typ:=top_reg;
  902. taicpu(hp).oper[0]^.reg:=NR_ST1;
  903. taicpu(hp).oper[1]^.typ:=top_reg;
  904. taicpu(hp).oper[1]^.reg:=NR_ST;
  905. end;
  906. { NASM only accepts move for loading of
  907. simple symbol address }
  908. if ((taicpu(hp).opcode=A_LEA) and
  909. (taicpu(hp).ops=2) and
  910. (taicpu(hp).oper[0]^.typ=top_reg) and
  911. (reg2opsize(taicpu(hp).oper[0]^.reg) in [S_NO,S_Q]) and
  912. (taicpu(hp).oper[1]^.typ=top_ref) and
  913. (taicpu(hp).oper[1]^.ref^.refaddr<>addr_no) and
  914. assigned(taicpu(hp).oper[1]^.ref^.symbol) and
  915. (taicpu(hp).oper[1]^.ref^.base=NR_NO)) then
  916. begin
  917. writer.AsmWrite(asminfo^.comment);
  918. writer.AsmWriteln('Converting LEA to MOV instruction');
  919. taicpu(hp).opcode:=A_MOV;
  920. end;
  921. if fixed_opcode=A_FWAIT then
  922. writer.AsmWriteln(#9#9'DB'#9'09bh')
  923. else if is_x86_parameterized_string_op(fixed_opcode) then
  924. begin
  925. writer.AsmWrite(#9#9);
  926. i:=get_x86_string_op_si_param(fixed_opcode);
  927. if (i<>-1) and (taicpu(hp).oper[i]^.typ=top_ref) and
  928. (taicpu(hp).oper[i]^.ref^.segment<>NR_NO) and
  929. (taicpu(hp).oper[i]^.ref^.segment<>NR_DS) then
  930. writer.AsmWrite(std_regname(taicpu(hp).oper[i]^.ref^.segment)+' ');
  931. for i:=0 to taicpu(hp).ops-1 do
  932. if taicpu(hp).oper[i]^.typ=top_ref then
  933. begin
  934. case get_ref_address_size(taicpu(hp).oper[i]^.ref^) of
  935. 16:
  936. writer.AsmWrite('a16 ');
  937. 32:
  938. writer.AsmWrite('a32 ');
  939. 64:
  940. writer.AsmWrite('a64 ');
  941. end;
  942. break;
  943. end;
  944. writer.AsmWrite(std_op2str[fixed_opcode]);
  945. case taicpu(hp).opsize of
  946. S_B:
  947. writer.AsmWrite('b');
  948. S_W:
  949. writer.AsmWrite('w');
  950. S_L:
  951. writer.AsmWrite('d');
  952. S_Q:
  953. writer.AsmWrite('q');
  954. else
  955. internalerror(2017101101);
  956. end;
  957. writer.AsmLn;
  958. end
  959. else
  960. begin
  961. prefix:='';
  962. {$ifndef i8086}
  963. { We need to explicitely set
  964. word prefix to get selectors
  965. to be pushed in 2 bytes PM }
  966. if (taicpu(hp).opsize=S_W) and
  967. ((fixed_opcode=A_PUSH) or
  968. (fixed_opcode=A_POP)) and
  969. (taicpu(hp).oper[0]^.typ=top_reg) and
  970. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  971. writer.AsmWriteln(#9#9'DB'#9'066h');
  972. {$endif not i8086}
  973. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  974. if taicpu(hp).ops<>0 then
  975. begin
  976. if is_calljmp(fixed_opcode) then
  977. begin
  978. writer.AsmWrite(#9);
  979. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp));
  980. end
  981. else
  982. begin
  983. for i:=0 to taicpu(hp).ops-1 do
  984. begin
  985. if i=0 then
  986. writer.AsmWrite(#9)
  987. else
  988. writer.AsmWrite(',');
  989. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  990. end;
  991. end;
  992. end;
  993. if not SkipNewLine then
  994. writer.AsmLn;
  995. end;
  996. end;
  997. ait_stab,
  998. ait_force_line,
  999. ait_function_name : ;
  1000. ait_cutobject :
  1001. begin
  1002. if SmartAsm then
  1003. begin
  1004. { only reset buffer if nothing has changed }
  1005. if not writer.ClearIfEmpty then
  1006. begin
  1007. if SmartAsm then
  1008. begin
  1009. WriteSmartExternals;
  1010. FreeExternChainList;
  1011. end;
  1012. WriteGroups;
  1013. writer.AsmClose;
  1014. DoAssemble;
  1015. writer.AsmCreate(tai_cutobject(hp).place);
  1016. ResetSectionsList;
  1017. WriteHeader;
  1018. end;
  1019. { avoid empty files }
  1020. LastSecType:=sec_none;
  1021. LastSecName:='';
  1022. LastAlign:=4;
  1023. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1024. begin
  1025. if tai(hp.next).typ=ait_section then
  1026. begin
  1027. LastSecType:=tai_section(hp.next).sectype;
  1028. LastSecName:=tai_section(hp.next).name^;
  1029. LastAlign:=tai_section(hp.next).secalign;
  1030. end;
  1031. hp:=tai(hp.next);
  1032. end;
  1033. if LastSecType<>sec_none then
  1034. WriteSection(LastSecType,LastSecName,LastAlign);
  1035. writer.MarkEmpty;
  1036. NewObject:=true;
  1037. end;
  1038. end;
  1039. ait_marker :
  1040. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1041. inc(InlineLevel)
  1042. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1043. dec(InlineLevel);
  1044. ait_directive :
  1045. begin
  1046. case tai_directive(hp).directive of
  1047. asd_nasm_import,
  1048. asd_extern :
  1049. begin
  1050. case tai_directive(hp).directive of
  1051. asd_nasm_import :
  1052. writer.AsmWrite('import ');
  1053. asd_extern :
  1054. writer.AsmWrite('EXTERN ');
  1055. else
  1056. internalerror(200509191);
  1057. end;
  1058. if tai_directive(hp).name<>'' then
  1059. begin
  1060. if SmartAsm then
  1061. AddSymbol(tai_directive(hp).name,false);
  1062. writer.AsmWrite(tai_directive(hp).name);
  1063. end;
  1064. end;
  1065. asd_cpu :
  1066. begin
  1067. writer.AsmWrite('CPU ');
  1068. for cpu:=low(tcputype) to high(tcputype) do
  1069. begin
  1070. if tai_directive(hp).name=CPUTypeStr[CPU] then
  1071. begin
  1072. writer.AsmWriteLn(nasm_cpu_name[cpu]);
  1073. break;
  1074. end;
  1075. end;
  1076. end;
  1077. else
  1078. internalerror(200509191);
  1079. end;
  1080. writer.AsmLn;
  1081. end;
  1082. ait_seh_directive :
  1083. { Ignore for now };
  1084. ait_varloc:
  1085. begin
  1086. if tai_varloc(hp).newlocationhi<>NR_NO then
  1087. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1088. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1089. else
  1090. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1091. std_regname(tai_varloc(hp).newlocation));
  1092. end;
  1093. else
  1094. internalerror(10000);
  1095. end;
  1096. hp:=tai(hp.next);
  1097. end;
  1098. end;
  1099. procedure TX86NasmAssembler.WriteExternals;
  1100. var
  1101. sym : TAsmSymbol;
  1102. i : longint;
  1103. begin
  1104. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  1105. begin
  1106. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1107. if sym.bind=AB_EXTERNAL then
  1108. writer.AsmWriteln('EXTERN'#9+sym.name);
  1109. end;
  1110. end;
  1111. procedure TX86NasmAssembler.WriteSmartExternals;
  1112. var
  1113. EC : PExternChain;
  1114. begin
  1115. EC:=FEC;
  1116. while assigned(EC) do
  1117. begin
  1118. if not EC^.is_defined then
  1119. writer.AsmWriteln('EXTERN'#9+EC^.psym^);
  1120. EC:=EC^.next;
  1121. end;
  1122. end;
  1123. procedure TX86NasmAssembler.WriteHeader;
  1124. begin
  1125. {$if defined(i8086)}
  1126. writer.AsmWriteLn('BITS 16');
  1127. {$elseif defined(i386)}
  1128. writer.AsmWriteLn('BITS 32');
  1129. using_relative:=false;
  1130. {$elseif defined(x86_64)}
  1131. writer.AsmWriteLn('BITS 64');
  1132. writer.AsmWriteLn('default rel');
  1133. using_relative:=true;
  1134. {$endif}
  1135. writer.AsmWriteLn('CPU '+nasm_cpu_name[current_settings.cputype]);
  1136. end;
  1137. procedure TX86NasmAssembler.WriteAsmList;
  1138. var
  1139. hal : tasmlisttype;
  1140. begin
  1141. {$ifdef EXTDEBUG}
  1142. if current_module.mainsource<>'' then
  1143. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1144. {$endif}
  1145. ResetSectionsList;
  1146. WriteHeader;
  1147. writer.AsmLn;
  1148. WriteExternals;
  1149. for hal:=low(TasmlistType) to high(TasmlistType) do
  1150. begin
  1151. if not (current_asmdata.asmlists[hal].empty) then
  1152. begin
  1153. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1154. writetree(current_asmdata.asmlists[hal]);
  1155. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1156. end;
  1157. end;
  1158. writer.AsmLn;
  1159. if SmartAsm then
  1160. begin
  1161. WriteSmartExternals;
  1162. FreeExternChainList;
  1163. end;
  1164. WriteGroups;
  1165. {$ifdef EXTDEBUG}
  1166. if current_module.mainsource<>'' then
  1167. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1168. {$endif EXTDEBUG}
  1169. end;
  1170. function TX86NasmAssembler.MakeCmdLine: TCmdStr;
  1171. var
  1172. FormatName : string;
  1173. begin
  1174. result:=Inherited MakeCmdLine;
  1175. {$ifdef i8086}
  1176. case target_info.system of
  1177. system_i8086_msdos,
  1178. system_i8086_win16,
  1179. system_i8086_embedded:
  1180. begin
  1181. FormatName:='obj';
  1182. if (cs_debuginfo in current_settings.moduleswitches) or
  1183. (cs_asm_source in current_settings.globalswitches) then
  1184. Replace(result,'$DEBUG','-g')
  1185. else
  1186. Replace(result,'$DEBUG','');
  1187. end
  1188. else
  1189. internalerror(2014082060);
  1190. end;
  1191. {$endif i8086}
  1192. {$ifdef i386}
  1193. case target_info.system of
  1194. system_i386_go32v2:
  1195. FormatName:='coff';
  1196. system_i386_wdosx,
  1197. system_i386_win32:
  1198. FormatName:='win32';
  1199. system_i386_embedded:
  1200. FormatName:='obj';
  1201. system_i386_linux,
  1202. system_i386_beos:
  1203. FormatName:='elf';
  1204. system_i386_darwin:
  1205. FormatName:='macho32';
  1206. else
  1207. FormatName:='elf';
  1208. end;
  1209. {$endif i386}
  1210. {$ifdef x86_64}
  1211. case target_info.system of
  1212. system_x86_64_win64:
  1213. FormatName:='win64';
  1214. system_x86_64_darwin:
  1215. FormatName:='macho64';
  1216. system_x86_64_embedded:
  1217. FormatName:='obj';
  1218. system_x86_64_linux:
  1219. FormatName:='elf64';
  1220. else
  1221. FormatName:='elf64';
  1222. end;
  1223. {$endif x86_64}
  1224. Replace(result,'$FORMAT',FormatName);
  1225. end;
  1226. {*****************************************************************************
  1227. Initialize
  1228. *****************************************************************************}
  1229. {$ifdef i8086}
  1230. const
  1231. as_i8086_nasm_info : tasminfo =
  1232. (
  1233. id : as_i8086_nasm;
  1234. idtxt : 'NASM';
  1235. asmbin : 'nasm';
  1236. asmcmd : '-f $FORMAT $DEBUG -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1237. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1238. flags : [af_needar,af_no_debug];
  1239. labelprefix : '..@';
  1240. comment : '; ';
  1241. dollarsign: '$';
  1242. );
  1243. as_i8086_nasmobj_info : tasminfo =
  1244. (
  1245. id : as_i8086_nasmobj;
  1246. idtxt : 'NASMOBJ';
  1247. asmbin : 'nasm';
  1248. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1249. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1250. flags : [af_needar,af_no_debug];
  1251. labelprefix : '..@';
  1252. comment : '; ';
  1253. dollarsign: '$';
  1254. );
  1255. {$endif i8086}
  1256. {$ifdef i386}
  1257. const
  1258. as_i386_nasmcoff_info : tasminfo =
  1259. (
  1260. id : as_i386_nasmcoff;
  1261. idtxt : 'NASMCOFF';
  1262. asmbin : 'nasm';
  1263. asmcmd : '-f coff -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1264. supported_targets : [system_i386_go32v2];
  1265. flags : [af_needar,af_no_debug];
  1266. labelprefix : '..@';
  1267. comment : '; ';
  1268. dollarsign: '$';
  1269. );
  1270. as_i386_nasmwin32_info : tasminfo =
  1271. (
  1272. id : as_i386_nasmwin32;
  1273. idtxt : 'NASMWIN32';
  1274. asmbin : 'nasm';
  1275. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1276. supported_targets : [system_i386_win32];
  1277. flags : [af_needar,af_no_debug,af_smartlink_sections];
  1278. labelprefix : '..@';
  1279. comment : '; ';
  1280. dollarsign: '$';
  1281. );
  1282. as_i386_nasmobj_info : tasminfo =
  1283. (
  1284. id : as_i386_nasmobj;
  1285. idtxt : 'NASMOBJ';
  1286. asmbin : 'nasm';
  1287. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1288. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1289. flags : [af_needar,af_no_debug];
  1290. labelprefix : '..@';
  1291. comment : '; ';
  1292. dollarsign: '$';
  1293. );
  1294. as_i386_nasmwdosx_info : tasminfo =
  1295. (
  1296. id : as_i386_nasmwdosx;
  1297. idtxt : 'NASMWDOSX';
  1298. asmbin : 'nasm';
  1299. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1300. supported_targets : [system_i386_wdosx];
  1301. flags : [af_needar,af_no_debug];
  1302. labelprefix : '..@';
  1303. comment : '; ';
  1304. dollarsign: '$';
  1305. );
  1306. as_i386_nasmelf_info : tasminfo =
  1307. (
  1308. id : as_i386_nasmelf;
  1309. idtxt : 'NASMELF';
  1310. asmbin : 'nasm';
  1311. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1312. supported_targets : [system_i386_linux];
  1313. flags : [af_needar,af_no_debug];
  1314. labelprefix : '..@';
  1315. comment : '; ';
  1316. dollarsign: '$';
  1317. );
  1318. {
  1319. as_i386_nasmdarwin_info : tasminfo =
  1320. (
  1321. id : as_i386_nasmdarwin;
  1322. idtxt : 'NASMDARWIN';
  1323. asmbin : 'nasm';
  1324. asmcmd : '-f macho32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1325. supported_targets : [system_i386_darwin];
  1326. flags : [af_needar,af_no_debug];
  1327. labelprefix : '..@';
  1328. comment : '; ';
  1329. dollarsign: '$';
  1330. );
  1331. }
  1332. as_i386_nasmbeos_info : tasminfo =
  1333. (
  1334. id : as_i386_nasmbeos;
  1335. idtxt : 'NASMELF';
  1336. asmbin : 'nasm';
  1337. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1338. supported_targets : [system_i386_beos];
  1339. flags : [af_needar,af_no_debug];
  1340. labelprefix : '..@';
  1341. comment : '; ';
  1342. dollarsign: '$';
  1343. );
  1344. as_i386_nasmhaiku_info : tasminfo =
  1345. (
  1346. id : as_i386_nasmhaiku;
  1347. idtxt : 'NASMELF';
  1348. asmbin : 'nasm';
  1349. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1350. supported_targets : [system_i386_haiku];
  1351. flags : [af_needar,af_no_debug];
  1352. labelprefix : '..@';
  1353. comment : '; ';
  1354. dollarsign: '$';
  1355. );
  1356. as_i386_nasm_info : tasminfo =
  1357. (
  1358. id : as_i386_nasm;
  1359. idtxt : 'NASM';
  1360. asmbin : 'nasm';
  1361. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1362. supported_targets : [system_any];
  1363. flags : [af_needar,af_no_debug];
  1364. labelprefix : '..@';
  1365. comment : '; ';
  1366. dollarsign: '$';
  1367. );
  1368. {$endif i386}
  1369. {$ifdef x86_64}
  1370. const
  1371. as_x86_64_nasm_info : tasminfo =
  1372. (
  1373. id : as_x86_64_nasm;
  1374. idtxt : 'NASM';
  1375. asmbin : 'nasm';
  1376. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1377. supported_targets : [system_any];
  1378. flags : [af_needar{,af_no_debug}];
  1379. labelprefix : '..@';
  1380. comment : '; ';
  1381. dollarsign: '$';
  1382. );
  1383. as_x86_64_nasmwin64_info : tasminfo =
  1384. (
  1385. id : as_x86_64_nasmwin64;
  1386. idtxt : 'NASMWIN64';
  1387. asmbin : 'nasm';
  1388. asmcmd : '-f win64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1389. supported_targets : [system_x86_64_win64];
  1390. flags : [af_needar,af_no_debug];
  1391. labelprefix : '..@';
  1392. comment : '; ';
  1393. dollarsign: '$';
  1394. );
  1395. as_x86_64_nasmelf_info : tasminfo =
  1396. (
  1397. id : as_x86_64_nasmelf;
  1398. idtxt : 'NASMELF';
  1399. asmbin : 'nasm';
  1400. asmcmd : '-f elf64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1401. supported_targets : [system_x86_64_linux];
  1402. flags : [af_needar,af_no_debug];
  1403. labelprefix : '..@';
  1404. comment : '; ';
  1405. dollarsign: '$';
  1406. );
  1407. as_x86_64_nasmdarwin_info : tasminfo =
  1408. (
  1409. id : as_x86_64_nasmdarwin;
  1410. idtxt : 'NASMDARWIN';
  1411. asmbin : 'nasm';
  1412. asmcmd : '-f macho64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1413. supported_targets : [system_x86_64_darwin];
  1414. flags : [af_needar,af_no_debug];
  1415. labelprefix : '..@';
  1416. comment : '; ';
  1417. dollarsign: '$';
  1418. );
  1419. {$endif x86_64}
  1420. initialization
  1421. {$ifdef i8086}
  1422. RegisterAssembler(as_i8086_nasm_info,TX86NasmAssembler);
  1423. RegisterAssembler(as_i8086_nasmobj_info,TX86NasmAssembler);
  1424. {$endif i8086}
  1425. {$ifdef i386}
  1426. RegisterAssembler(as_i386_nasmcoff_info,TX86NasmAssembler);
  1427. RegisterAssembler(as_i386_nasmwin32_info,TX86NasmAssembler);
  1428. RegisterAssembler(as_i386_nasmwdosx_info,TX86NasmAssembler);
  1429. RegisterAssembler(as_i386_nasmobj_info,TX86NasmAssembler);
  1430. RegisterAssembler(as_i386_nasmbeos_info,TX86NasmAssembler);
  1431. RegisterAssembler(as_i386_nasmhaiku_info,TX86NasmAssembler);
  1432. RegisterAssembler(as_i386_nasmelf_info,TX86NasmAssembler);
  1433. RegisterAssembler(as_i386_nasm_info,TX86NasmAssembler);
  1434. {$endif i386}
  1435. {$ifdef x86_64}
  1436. RegisterAssembler(as_x86_64_nasm_info,TX86NasmAssembler);
  1437. RegisterAssembler(as_x86_64_nasmwin64_info,TX86NasmAssembler);
  1438. RegisterAssembler(as_x86_64_nasmelf_info,TX86NasmAssembler);
  1439. RegisterAssembler(as_x86_64_nasmdarwin_info,TX86NasmAssembler);
  1440. {$endif x86_64}
  1441. end.