agx86nsm.pas 52 KB

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