agx86nsm.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  24. type
  25. T386NasmAssembler = class(texternalassembler)
  26. private
  27. procedure WriteReference(var ref : treference);
  28. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  29. procedure WriteOper_jmp(const o:toper; op : tasmop);
  30. procedure WriteSection(atype:TAsmSectiontype;const aname:string);
  31. public
  32. procedure WriteTree(p:TAsmList);override;
  33. procedure WriteAsmList;override;
  34. procedure WriteExternals;
  35. procedure WriteSmartExternals;
  36. end;
  37. implementation
  38. uses
  39. cutils,globtype,globals,systems,cclasses,
  40. fmodule,finput,verbose,cpuinfo,cgbase
  41. ;
  42. type
  43. {$ifdef cpuextended}
  44. t80bitarray = array[0..9] of byte;
  45. {$endif cpuextended}
  46. t64bitarray = array[0..7] of byte;
  47. t32bitarray = array[0..3] of byte;
  48. const
  49. line_length = 64;
  50. nasm_regname_table : array[tregisterindex] of string[7] = (
  51. {r386nasm.inc contains the Nasm name of each register.}
  52. {$i r386nasm.inc}
  53. );
  54. function nasm_regname(r:Tregister):string;
  55. var
  56. p : tregisterindex;
  57. begin
  58. p:=findreg_by_number(r);
  59. if p<>0 then
  60. result:=nasm_regname_table[p]
  61. else
  62. result:=generic_regname(r);
  63. end;
  64. function single2str(d : single) : string;
  65. var
  66. hs : string;
  67. p : longint;
  68. begin
  69. str(d,hs);
  70. { nasm expects a lowercase e }
  71. p:=pos('E',hs);
  72. if p>0 then
  73. hs[p]:='e';
  74. p:=pos('+',hs);
  75. if p>0 then
  76. delete(hs,p,1);
  77. single2str:=lower(hs);
  78. end;
  79. function double2str(d : double) : string;
  80. var
  81. hs : string;
  82. p : longint;
  83. begin
  84. str(d,hs);
  85. { nasm expects a lowercase e }
  86. p:=pos('E',hs);
  87. if p>0 then
  88. hs[p]:='e';
  89. p:=pos('+',hs);
  90. if p>0 then
  91. delete(hs,p,1);
  92. double2str:=lower(hs);
  93. end;
  94. function extended2str(e : extended) : string;
  95. var
  96. hs : string;
  97. p : longint;
  98. begin
  99. str(e,hs);
  100. { nasm expects a lowercase e }
  101. p:=pos('E',hs);
  102. if p>0 then
  103. hs[p]:='e';
  104. p:=pos('+',hs);
  105. if p>0 then
  106. delete(hs,p,1);
  107. extended2str:=lower(hs);
  108. end;
  109. { convert floating point values }
  110. { to correct endian }
  111. procedure swap64bitarray(var t: t64bitarray);
  112. var
  113. b: byte;
  114. begin
  115. b:= t[7];
  116. t[7] := t[0];
  117. t[0] := b;
  118. b := t[6];
  119. t[6] := t[1];
  120. t[1] := b;
  121. b:= t[5];
  122. t[5] := t[2];
  123. t[2] := b;
  124. b:= t[4];
  125. t[4] := t[3];
  126. t[3] := b;
  127. end;
  128. procedure swap32bitarray(var t: t32bitarray);
  129. var
  130. b: byte;
  131. begin
  132. b:= t[1];
  133. t[1]:= t[2];
  134. t[2]:= b;
  135. b:= t[0];
  136. t[0]:= t[3];
  137. t[3]:= b;
  138. end;
  139. function comp2str(d : bestreal) : string;
  140. type
  141. pdouble = ^double;
  142. var
  143. c : comp;
  144. dd : pdouble;
  145. begin
  146. c:=comp(d);
  147. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  148. comp2str:=double2str(dd^);
  149. end;
  150. function sizestr(s:topsize;dest:boolean):string;
  151. begin
  152. case s of
  153. S_B : sizestr:='byte ';
  154. S_W : sizestr:='word ';
  155. S_L : sizestr:='dword ';
  156. S_Q : sizestr:='qword ';
  157. S_IS : sizestr:='word ';
  158. S_IL : sizestr:='dword ';
  159. S_IQ : sizestr:='qword ';
  160. S_FS : sizestr:='dword ';
  161. S_FL : sizestr:='qword ';
  162. S_FX : sizestr:='tword ';
  163. S_BW : if dest then
  164. sizestr:='word '
  165. else
  166. sizestr:='byte ';
  167. S_BL : if dest then
  168. sizestr:='dword '
  169. else
  170. sizestr:='byte ';
  171. S_WL : if dest then
  172. sizestr:='dword '
  173. else
  174. sizestr:='word ';
  175. else { S_NO }
  176. sizestr:='';
  177. end;
  178. end;
  179. Function PadTabs(const p:string;addch:char):string;
  180. var
  181. s : string;
  182. i : longint;
  183. begin
  184. i:=length(p);
  185. if addch<>#0 then
  186. begin
  187. inc(i);
  188. s:=p+addch;
  189. end
  190. else
  191. s:=p;
  192. if i<8 then
  193. PadTabs:=s+#9#9
  194. else
  195. PadTabs:=s+#9;
  196. end;
  197. type
  198. PExternChain = ^TExternChain;
  199. TExternChain = Record
  200. psym : pshortstring;
  201. is_defined : boolean;
  202. next : PExternChain;
  203. end;
  204. const
  205. FEC : PExternChain = nil;
  206. procedure AddSymbol(symname : string; defined : boolean);
  207. var
  208. EC : PExternChain;
  209. begin
  210. EC:=FEC;
  211. while assigned(EC) do
  212. begin
  213. if EC^.psym^=symname then
  214. begin
  215. if defined then
  216. EC^.is_defined:=true;
  217. exit;
  218. end;
  219. EC:=EC^.next;
  220. end;
  221. New(EC);
  222. EC^.next:=FEC;
  223. FEC:=EC;
  224. FEC^.psym:=stringdup(symname);
  225. FEC^.is_defined := defined;
  226. end;
  227. procedure FreeExternChainList;
  228. var
  229. EC : PExternChain;
  230. begin
  231. EC:=FEC;
  232. while assigned(EC) do
  233. begin
  234. FEC:=EC^.next;
  235. stringdispose(EC^.psym);
  236. Dispose(EC);
  237. EC:=FEC;
  238. end;
  239. end;
  240. {****************************************************************************
  241. T386NasmAssembler
  242. ****************************************************************************}
  243. procedure T386NasmAssembler.WriteReference(var ref : treference);
  244. var
  245. first : boolean;
  246. begin
  247. with ref do
  248. begin
  249. AsmWrite('[');
  250. first:=true;
  251. if (segment<>NR_NO) then
  252. AsmWrite(nasm_regname(segment)+':');
  253. if assigned(symbol) then
  254. begin
  255. AsmWrite(symbol.name);
  256. if SmartAsm then
  257. AddSymbol(symbol.name,false);
  258. first:=false;
  259. end;
  260. if (base<>NR_NO) then
  261. begin
  262. if not(first) then
  263. AsmWrite('+')
  264. else
  265. first:=false;
  266. AsmWrite(nasm_regname(base))
  267. end;
  268. if (index<>NR_NO) then
  269. begin
  270. if not(first) then
  271. AsmWrite('+')
  272. else
  273. first:=false;
  274. AsmWrite(nasm_regname(index));
  275. if scalefactor<>0 then
  276. AsmWrite('*'+tostr(scalefactor));
  277. end;
  278. if offset<0 then
  279. begin
  280. AsmWrite(tostr(offset));
  281. first:=false;
  282. end
  283. else if (offset>0) then
  284. begin
  285. AsmWrite('+'+tostr(offset));
  286. first:=false;
  287. end;
  288. if first then
  289. AsmWrite('0');
  290. AsmWrite(']');
  291. end;
  292. end;
  293. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  294. begin
  295. case o.typ of
  296. top_reg :
  297. AsmWrite(nasm_regname(o.reg));
  298. top_const :
  299. begin
  300. if (ops=1) and (opcode<>A_RET) then
  301. AsmWrite(sizestr(s,dest));
  302. AsmWrite(tostr(longint(o.val)));
  303. end;
  304. top_ref :
  305. begin
  306. if o.ref^.refaddr=addr_no then
  307. begin
  308. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  309. (opcode = A_LSS) or (opcode = A_LFS) or
  310. (opcode = A_LES) or (opcode = A_LDS) or
  311. // (opcode = A_SHR) or (opcode = A_SHL) or
  312. (opcode = A_SAR) or (opcode = A_SAL) or
  313. (opcode = A_OUT) or (opcode = A_IN)) then
  314. AsmWrite(sizestr(s,dest));
  315. WriteReference(o.ref^);
  316. end
  317. else
  318. begin
  319. {$ifdef x86_64}
  320. asmwrite('qword ');
  321. {$endif}
  322. {$ifdef i386}
  323. asmwrite('dword ');
  324. {$endif i386}
  325. {$ifdef i8086}
  326. asmwrite('word ');
  327. {$endif i8086}
  328. if assigned(o.ref^.symbol) then
  329. begin
  330. if SmartAsm then
  331. AddSymbol(o.ref^.symbol.name,false);
  332. asmwrite(o.ref^.symbol.name);
  333. if o.ref^.offset=0 then
  334. exit;
  335. end;
  336. if o.ref^.offset>0 then
  337. asmwrite('+');
  338. asmwrite(tostr(o.ref^.offset));
  339. end;
  340. end;
  341. else
  342. internalerror(10001);
  343. end;
  344. end;
  345. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  346. begin
  347. case o.typ of
  348. top_reg :
  349. AsmWrite(nasm_regname(o.reg));
  350. top_ref :
  351. if o.ref^.refaddr=addr_no then
  352. WriteReference(o.ref^)
  353. else
  354. begin
  355. if not(
  356. (op=A_JCXZ) or (op=A_JECXZ) or
  357. {$ifdef x86_64}
  358. (op=A_JRCXZ) or
  359. {$endif x86_64}
  360. (op=A_LOOP) or (op=A_LOOPE) or
  361. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  362. (op=A_LOOPZ)
  363. ) then
  364. AsmWrite('NEAR ');
  365. AsmWrite(o.ref^.symbol.name);
  366. if SmartAsm then
  367. AddSymbol(o.ref^.symbol.name,false);
  368. if o.ref^.offset>0 then
  369. AsmWrite('+'+tostr(o.ref^.offset))
  370. else
  371. if o.ref^.offset<0 then
  372. AsmWrite(tostr(o.ref^.offset));
  373. end;
  374. top_const :
  375. AsmWrite(tostr(aint(o.val)));
  376. else
  377. internalerror(10001);
  378. end;
  379. end;
  380. const
  381. ait_const2str : array[aitconst_128bit..aitconst_secrel32_symbol] of string[20]=(
  382. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  383. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  384. #9'RVA'#9,#9'SECREL32'#9
  385. );
  386. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  387. const
  388. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  389. '.text',
  390. '.data',
  391. '.data',
  392. '.rodata',
  393. '.bss',
  394. '.tbss',
  395. '.pdata',
  396. '.text','.data','.data','.data','.data',
  397. '.stab',
  398. '.stabstr',
  399. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  400. '.eh_frame',
  401. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  402. '.fpc',
  403. '',
  404. '.init',
  405. '.fini',
  406. '.objc_class',
  407. '.objc_meta_class',
  408. '.objc_cat_cls_meth',
  409. '.objc_cat_inst_meth',
  410. '.objc_protocol',
  411. '.objc_string_object',
  412. '.objc_cls_meth',
  413. '.objc_inst_meth',
  414. '.objc_cls_refs',
  415. '.objc_message_refs',
  416. '.objc_symbols',
  417. '.objc_category',
  418. '.objc_class_vars',
  419. '.objc_instance_vars',
  420. '.objc_module_info',
  421. '.objc_class_names',
  422. '.objc_meth_var_types',
  423. '.objc_meth_var_names',
  424. '.objc_selector_strs',
  425. '.objc_protocol_ext',
  426. '.objc_class_ext',
  427. '.objc_property',
  428. '.objc_image_info',
  429. '.objc_cstring_object',
  430. '.objc_sel_fixup',
  431. '__DATA,__objc_data',
  432. '__DATA,__objc_const',
  433. '.objc_superrefs',
  434. '__DATA, __datacoal_nt,coalesced',
  435. '.objc_classlist',
  436. '.objc_nlclasslist',
  437. '.objc_catlist',
  438. '.obcj_nlcatlist',
  439. '.objc_protolist'
  440. );
  441. begin
  442. AsmLn;
  443. AsmWrite('SECTION ');
  444. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  445. Thus, data which normally goes into .rodata and .rodata_norel sections must
  446. end up in .data section }
  447. if (atype in [sec_rodata,sec_rodata_norel]) and
  448. (target_info.system=system_i386_go32v2) then
  449. AsmWrite('.data')
  450. else
  451. AsmWrite(secnames[atype]);
  452. if create_smartlink_sections and
  453. (atype<>sec_bss) and
  454. (aname<>'') then
  455. begin
  456. AsmWrite('.');
  457. AsmWrite(aname);
  458. end;
  459. AsmLn;
  460. LasTSecType:=atype;
  461. end;
  462. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  463. {$ifdef cpuextended}
  464. type
  465. t80bitarray = array[0..9] of byte;
  466. {$endif cpuextended}
  467. var
  468. s : string;
  469. hp : tai;
  470. counter,
  471. lines,
  472. i,j,l : longint;
  473. InlineLevel : longint;
  474. consttype : taiconst_type;
  475. do_line,
  476. quoted : boolean;
  477. co : comp;
  478. sin : single;
  479. d : double;
  480. {$ifdef cpuextended}
  481. e : extended;
  482. {$endif cpuextended}
  483. begin
  484. if not assigned(p) then
  485. exit;
  486. InlineLevel:=0;
  487. { lineinfo is only needed for al_procedures (PFV) }
  488. do_line:=(cs_asm_source in current_settings.globalswitches) or
  489. ((cs_lineinfo in current_settings.moduleswitches)
  490. and (p=current_asmdata.asmlists[al_procedures]));
  491. hp:=tai(p.first);
  492. while assigned(hp) do
  493. begin
  494. prefetch(pointer(hp.next)^);
  495. if not(hp.typ in SkipLineInfo) then
  496. begin
  497. current_filepos:=tailineinfo(hp).fileinfo;
  498. { no line info for inlined code }
  499. if do_line and (inlinelevel=0) then
  500. WriteSourceLine(hp as tailineinfo);
  501. end;
  502. case hp.typ of
  503. ait_comment :
  504. Begin
  505. AsmWrite(target_asm.comment);
  506. AsmWritePChar(tai_comment(hp).str);
  507. AsmLn;
  508. End;
  509. ait_regalloc :
  510. begin
  511. if (cs_asm_regalloc in current_settings.globalswitches) then
  512. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  513. regallocstr[tai_regalloc(hp).ratype]);
  514. end;
  515. ait_tempalloc :
  516. begin
  517. if (cs_asm_tempalloc in current_settings.globalswitches) then
  518. WriteTempalloc(tai_tempalloc(hp));
  519. end;
  520. ait_section :
  521. begin
  522. if tai_section(hp).sectype<>sec_none then
  523. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  524. LasTSecType:=tai_section(hp).sectype;
  525. end;
  526. ait_align :
  527. begin
  528. { nasm gives warnings when it finds align in bss as it
  529. wants to store data }
  530. if (lastsectype<>sec_bss) and
  531. (tai_align(hp).aligntype>1) then
  532. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  533. end;
  534. ait_datablock :
  535. begin
  536. if tai_datablock(hp).is_global or SmartAsm then
  537. begin
  538. AsmWrite(#9'GLOBAL ');
  539. AsmWriteLn(tai_datablock(hp).sym.name);
  540. end;
  541. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  542. if SmartAsm then
  543. AddSymbol(tai_datablock(hp).sym.name,true);
  544. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  545. end;
  546. ait_const:
  547. begin
  548. consttype:=tai_const(hp).consttype;
  549. case consttype of
  550. aitconst_64bit :
  551. begin
  552. if assigned(tai_const(hp).sym) then
  553. internalerror(200404292);
  554. AsmWrite(ait_const2str[aitconst_32bit]);
  555. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  556. AsmWrite(',');
  557. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  558. AsmLn;
  559. end;
  560. aitconst_uleb128bit,
  561. aitconst_sleb128bit,
  562. aitconst_128bit:
  563. begin
  564. end;
  565. aitconst_32bit,
  566. aitconst_16bit,
  567. aitconst_8bit,
  568. aitconst_rva_symbol,
  569. aitconst_secrel32_symbol :
  570. begin
  571. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  572. l:=0;
  573. repeat
  574. if assigned(tai_const(hp).sym) then
  575. begin
  576. if SmartAsm then
  577. begin
  578. AddSymbol(tai_const(hp).sym.name,false);
  579. if assigned(tai_const(hp).endsym) then
  580. AddSymbol(tai_const(hp).endsym.name,false);
  581. end;
  582. if assigned(tai_const(hp).endsym) then
  583. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  584. else
  585. s:=tai_const(hp).sym.name;
  586. if tai_const(hp).value<>0 then
  587. s:=s+tostr_with_plus(tai_const(hp).value);
  588. end
  589. else
  590. s:=tostr(tai_const(hp).value);
  591. AsmWrite(s);
  592. inc(l,length(s));
  593. if (l>line_length) or
  594. (hp.next=nil) or
  595. (tai(hp.next).typ<>ait_const) or
  596. (tai_const(hp.next).consttype<>consttype) then
  597. break;
  598. hp:=tai(hp.next);
  599. AsmWrite(',');
  600. until false;
  601. AsmLn;
  602. end;
  603. else
  604. internalerror(200704252);
  605. end;
  606. end;
  607. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  608. ait_real_80bit :
  609. begin
  610. if do_line then
  611. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  612. { Make sure e is a extended type, bestreal could be
  613. a different type (bestreal) !! (PFV) }
  614. e:=tai_real_80bit(hp).value;
  615. AsmWrite(#9#9'DB'#9);
  616. for i:=0 to 9 do
  617. begin
  618. if i<>0 then
  619. AsmWrite(',');
  620. AsmWrite(tostr(t80bitarray(e)[i]));
  621. end;
  622. for i:=11 to tai_real_80bit(hp).savesize do
  623. AsmWrite(',0');
  624. AsmLn;
  625. end;
  626. {$else cpuextended}
  627. ait_real_80bit :
  628. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  629. {$endif cpuextended}
  630. // ait_real_64bit :
  631. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  632. ait_real_64bit :
  633. begin
  634. if do_line then
  635. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  636. d:=tai_real_64bit(hp).value;
  637. { swap the values to correct endian if required }
  638. if source_info.endian <> target_info.endian then
  639. swap64bitarray(t64bitarray(d));
  640. AsmWrite(#9#9'DB'#9);
  641. {$ifdef arm}
  642. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  643. begin
  644. for i:=4 to 7 do
  645. begin
  646. if i<>4 then
  647. AsmWrite(',');
  648. AsmWrite(tostr(t64bitarray(d)[i]));
  649. end;
  650. for i:=0 to 3 do
  651. begin
  652. AsmWrite(',');
  653. AsmWrite(tostr(t64bitarray(d)[i]));
  654. end;
  655. end
  656. else
  657. {$endif arm}
  658. begin
  659. for i:=0 to 7 do
  660. begin
  661. if i<>0 then
  662. AsmWrite(',');
  663. AsmWrite(tostr(t64bitarray(d)[i]));
  664. end;
  665. end;
  666. AsmLn;
  667. end;
  668. // ait_real_32bit :
  669. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  670. ait_real_32bit :
  671. begin
  672. if do_line then
  673. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  674. sin:=tai_real_32bit(hp).value;
  675. { swap the values to correct endian if required }
  676. if source_info.endian <> target_info.endian then
  677. swap32bitarray(t32bitarray(sin));
  678. AsmWrite(#9#9'DB'#9);
  679. for i:=0 to 3 do
  680. begin
  681. if i<>0 then
  682. AsmWrite(',');
  683. AsmWrite(tostr(t32bitarray(sin)[i]));
  684. end;
  685. AsmLn;
  686. end;
  687. // ait_comp_64bit :
  688. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  689. ait_comp_64bit :
  690. begin
  691. if do_line then
  692. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  693. AsmWrite(#9#9'DB'#9);
  694. co:=comp(tai_comp_64bit(hp).value);
  695. { swap the values to correct endian if required }
  696. if source_info.endian <> target_info.endian then
  697. swap64bitarray(t64bitarray(co));
  698. for i:=0 to 7 do
  699. begin
  700. if i<>0 then
  701. AsmWrite(',');
  702. AsmWrite(tostr(t64bitarray(co)[i]));
  703. end;
  704. AsmLn;
  705. end;
  706. ait_string :
  707. begin
  708. counter := 0;
  709. lines := tai_string(hp).len div line_length;
  710. { separate lines in different parts }
  711. if tai_string(hp).len > 0 then
  712. Begin
  713. for j := 0 to lines-1 do
  714. begin
  715. AsmWrite(#9#9'DB'#9);
  716. quoted:=false;
  717. for i:=counter to counter+line_length-1 do
  718. begin
  719. { it is an ascii character. }
  720. if (ord(tai_string(hp).str[i])>31) and
  721. (ord(tai_string(hp).str[i])<128) and
  722. (tai_string(hp).str[i]<>'"') then
  723. begin
  724. if not(quoted) then
  725. begin
  726. if i>counter then
  727. AsmWrite(',');
  728. AsmWrite('"');
  729. end;
  730. AsmWrite(tai_string(hp).str[i]);
  731. quoted:=true;
  732. end { if > 31 and < 128 and ord('"') }
  733. else
  734. begin
  735. if quoted then
  736. AsmWrite('"');
  737. if i>counter then
  738. AsmWrite(',');
  739. quoted:=false;
  740. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  741. end;
  742. end; { end for i:=0 to... }
  743. if quoted then AsmWrite('"');
  744. AsmWrite(target_info.newline);
  745. inc(counter,line_length);
  746. end; { end for j:=0 ... }
  747. { do last line of lines }
  748. if counter<tai_string(hp).len then
  749. AsmWrite(#9#9'DB'#9);
  750. quoted:=false;
  751. for i:=counter to tai_string(hp).len-1 do
  752. begin
  753. { it is an ascii character. }
  754. if (ord(tai_string(hp).str[i])>31) and
  755. (ord(tai_string(hp).str[i])<128) and
  756. (tai_string(hp).str[i]<>'"') then
  757. begin
  758. if not(quoted) then
  759. begin
  760. if i>counter then
  761. AsmWrite(',');
  762. AsmWrite('"');
  763. end;
  764. AsmWrite(tai_string(hp).str[i]);
  765. quoted:=true;
  766. end { if > 31 and < 128 and " }
  767. else
  768. begin
  769. if quoted then
  770. AsmWrite('"');
  771. if i>counter then
  772. AsmWrite(',');
  773. quoted:=false;
  774. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  775. end;
  776. end; { end for i:=0 to... }
  777. if quoted then
  778. AsmWrite('"');
  779. end;
  780. AsmLn;
  781. end;
  782. ait_label :
  783. begin
  784. if tai_label(hp).labsym.is_used then
  785. begin
  786. if SmartAsm then
  787. begin
  788. AsmWrite(#9'GLOBAL ');
  789. AsmWriteLn(tai_label(hp).labsym.name);
  790. end;
  791. AsmWriteLn(tai_label(hp).labsym.name+':');
  792. end;
  793. if SmartAsm then
  794. AddSymbol(tai_label(hp).labsym.name,true);
  795. end;
  796. ait_symbol :
  797. begin
  798. if tai_symbol(hp).has_value then
  799. internalerror(2009090803);
  800. if tai_symbol(hp).is_global or SmartAsm then
  801. begin
  802. AsmWrite(#9'GLOBAL ');
  803. AsmWriteLn(tai_symbol(hp).sym.name);
  804. end;
  805. AsmWrite(tai_symbol(hp).sym.name);
  806. if SmartAsm then
  807. AddSymbol(tai_symbol(hp).sym.name,true);
  808. if assigned(hp.next) and not(tai(hp.next).typ in
  809. [ait_const,
  810. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  811. AsmWriteLn(':')
  812. end;
  813. ait_symbol_end : ;
  814. ait_instruction :
  815. begin
  816. taicpu(hp).CheckNonCommutativeOpcodes;
  817. { We need intel order, no At&t }
  818. taicpu(hp).SetOperandOrder(op_intel);
  819. s:='';
  820. if ((taicpu(hp).opcode=A_FADDP) or
  821. (taicpu(hp).opcode=A_FMULP))
  822. and (taicpu(hp).ops=0) then
  823. begin
  824. taicpu(hp).allocate_oper(2);
  825. taicpu(hp).oper[0]^.typ:=top_reg;
  826. taicpu(hp).oper[0]^.reg:=NR_ST1;
  827. taicpu(hp).oper[1]^.typ:=top_reg;
  828. taicpu(hp).oper[1]^.reg:=NR_ST;
  829. end;
  830. if taicpu(hp).opcode=A_FWAIT then
  831. AsmWriteln(#9#9'DB'#9'09bh')
  832. else
  833. begin
  834. { We need to explicitely set
  835. word prefix to get selectors
  836. to be pushed in 2 bytes PM }
  837. if (taicpu(hp).opsize=S_W) and
  838. ((taicpu(hp).opcode=A_PUSH) or
  839. (taicpu(hp).opcode=A_POP)) and
  840. (taicpu(hp).oper[0]^.typ=top_reg) and
  841. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  842. AsmWriteln(#9#9'DB'#9'066h');
  843. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  844. if taicpu(hp).ops<>0 then
  845. begin
  846. if is_calljmp(taicpu(hp).opcode) then
  847. begin
  848. AsmWrite(#9);
  849. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  850. end
  851. else
  852. begin
  853. for i:=0 to taicpu(hp).ops-1 do
  854. begin
  855. if i=0 then
  856. AsmWrite(#9)
  857. else
  858. AsmWrite(',');
  859. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  860. end;
  861. end;
  862. end;
  863. AsmLn;
  864. end;
  865. end;
  866. ait_stab,
  867. ait_force_line,
  868. ait_function_name : ;
  869. ait_cutobject :
  870. begin
  871. if SmartAsm then
  872. begin
  873. { only reset buffer if nothing has changed }
  874. if AsmSize=AsmStartSize then
  875. AsmClear
  876. else
  877. begin
  878. if SmartAsm then
  879. begin
  880. WriteSmartExternals;
  881. FreeExternChainList;
  882. end;
  883. AsmClose;
  884. DoAssemble;
  885. AsmCreate(tai_cutobject(hp).place);
  886. end;
  887. { avoid empty files }
  888. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  889. begin
  890. if tai(hp.next).typ=ait_section then
  891. lasTSectype:=tai_section(hp.next).sectype;
  892. hp:=tai(hp.next);
  893. end;
  894. if lasTSectype<>sec_none then
  895. WriteSection(lasTSectype,'');
  896. AsmStartSize:=AsmSize;
  897. end;
  898. end;
  899. ait_marker :
  900. if tai_marker(hp).kind=mark_NoLineInfoStart then
  901. inc(InlineLevel)
  902. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  903. dec(InlineLevel);
  904. ait_directive :
  905. begin
  906. case tai_directive(hp).directive of
  907. asd_nasm_import :
  908. AsmWrite('import ');
  909. asd_extern :
  910. AsmWrite('EXTERN ');
  911. else
  912. internalerror(200509191);
  913. end;
  914. if tai_directive(hp).name<>'' then
  915. begin
  916. if SmartAsm then
  917. AddSymbol(tai_directive(hp).name,false);
  918. AsmWrite(tai_directive(hp).name);
  919. end;
  920. AsmLn;
  921. end;
  922. ait_seh_directive :
  923. { Ignore for now };
  924. else
  925. internalerror(10000);
  926. end;
  927. hp:=tai(hp.next);
  928. end;
  929. end;
  930. procedure T386NasmAssembler.WriteExternals;
  931. var
  932. sym : TAsmSymbol;
  933. i : longint;
  934. begin
  935. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  936. begin
  937. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  938. if sym.bind=AB_EXTERNAL then
  939. AsmWriteln('EXTERN'#9+sym.name);
  940. end;
  941. end;
  942. procedure T386NasmAssembler.WriteSmartExternals;
  943. var
  944. EC : PExternChain;
  945. begin
  946. EC:=FEC;
  947. while assigned(EC) do
  948. begin
  949. if not EC^.is_defined then
  950. AsmWriteln('EXTERN'#9+EC^.psym^);
  951. EC:=EC^.next;
  952. end;
  953. end;
  954. procedure T386NasmAssembler.WriteAsmList;
  955. var
  956. hal : tasmlisttype;
  957. begin
  958. {$ifdef EXTDEBUG}
  959. if current_module.mainsource<>'' then
  960. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  961. {$endif}
  962. {$ifdef i8086}
  963. AsmWriteLn('BITS 16');
  964. {$else i8086}
  965. AsmWriteLn('BITS 32');
  966. {$endif i8086}
  967. AsmLn;
  968. WriteExternals;
  969. for hal:=low(TasmlistType) to high(TasmlistType) do
  970. begin
  971. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  972. writetree(current_asmdata.asmlists[hal]);
  973. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  974. end;
  975. AsmLn;
  976. if SmartAsm then
  977. begin
  978. WriteSmartExternals;
  979. FreeExternChainList;
  980. end;
  981. {$ifdef i8086}
  982. { NASM complains if you put a missing section in the GROUP directive, so }
  983. { we add empty declarations to make sure they exist, even if empty }
  984. AsmWriteLn('SECTION .rodata');
  985. AsmWriteLn('SECTION .data');
  986. AsmWriteLn('SECTION .bss');
  987. { group these sections in the same segment }
  988. AsmWriteLn('GROUP dgroup rodata data bss');
  989. {$endif i8086}
  990. {$ifdef EXTDEBUG}
  991. if current_module.mainsource<>'' then
  992. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  993. {$endif EXTDEBUG}
  994. end;
  995. {*****************************************************************************
  996. Initialize
  997. *****************************************************************************}
  998. const
  999. as_i386_nasmcoff_info : tasminfo =
  1000. (
  1001. id : as_i386_nasmcoff;
  1002. idtxt : 'NASMCOFF';
  1003. asmbin : 'nasm';
  1004. asmcmd : '-f coff -o $OBJ $ASM';
  1005. supported_targets : [system_i386_go32v2];
  1006. flags : [af_needar,af_no_debug];
  1007. labelprefix : '..@';
  1008. comment : '; ';
  1009. dollarsign: '$';
  1010. );
  1011. as_i386_nasmwin32_info : tasminfo =
  1012. (
  1013. id : as_i386_nasmwin32;
  1014. idtxt : 'NASMWIN32';
  1015. asmbin : 'nasm';
  1016. asmcmd : '-f win32 -o $OBJ $ASM';
  1017. supported_targets : [system_i386_win32];
  1018. flags : [af_needar,af_no_debug];
  1019. labelprefix : '..@';
  1020. comment : '; ';
  1021. dollarsign: '$';
  1022. );
  1023. as_i386_nasmobj_info : tasminfo =
  1024. (
  1025. id : as_i386_nasmobj;
  1026. idtxt : 'NASMOBJ';
  1027. asmbin : 'nasm';
  1028. asmcmd : '-f obj -o $OBJ $ASM';
  1029. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1030. flags : [af_needar,af_no_debug];
  1031. labelprefix : '..@';
  1032. comment : '; ';
  1033. dollarsign: '$';
  1034. );
  1035. as_i386_nasmwdosx_info : tasminfo =
  1036. (
  1037. id : as_i386_nasmwdosx;
  1038. idtxt : 'NASMWDOSX';
  1039. asmbin : 'nasm';
  1040. asmcmd : '-f win32 -o $OBJ $ASM';
  1041. supported_targets : [system_i386_wdosx];
  1042. flags : [af_needar,af_no_debug];
  1043. labelprefix : '..@';
  1044. comment : '; ';
  1045. dollarsign: '$';
  1046. );
  1047. as_i386_nasmelf_info : tasminfo =
  1048. (
  1049. id : as_i386_nasmelf;
  1050. idtxt : 'NASMELF';
  1051. asmbin : 'nasm';
  1052. asmcmd : '-f elf -o $OBJ $ASM';
  1053. supported_targets : [system_i386_linux];
  1054. flags : [af_needar,af_no_debug];
  1055. labelprefix : '..@';
  1056. comment : '; ';
  1057. dollarsign: '$';
  1058. );
  1059. as_i386_nasmbeos_info : tasminfo =
  1060. (
  1061. id : as_i386_nasmbeos;
  1062. idtxt : 'NASMELF';
  1063. asmbin : 'nasm';
  1064. asmcmd : '-f elf -o $OBJ $ASM';
  1065. supported_targets : [system_i386_beos];
  1066. flags : [af_needar,af_no_debug];
  1067. labelprefix : '..@';
  1068. comment : '; ';
  1069. dollarsign: '$';
  1070. );
  1071. as_i386_nasmhaiku_info : tasminfo =
  1072. (
  1073. id : as_i386_nasmhaiku;
  1074. idtxt : 'NASMELF';
  1075. asmbin : 'nasm';
  1076. asmcmd : '-f elf -o $OBJ $ASM';
  1077. supported_targets : [system_i386_haiku];
  1078. flags : [af_needar,af_no_debug];
  1079. labelprefix : '..@';
  1080. comment : '; ';
  1081. dollarsign: '$';
  1082. );
  1083. initialization
  1084. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1085. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1086. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1087. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1088. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1089. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1090. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1091. end.