agx86nsm.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. asmwrite('dword ');
  320. if assigned(o.ref^.symbol) then
  321. begin
  322. if SmartAsm then
  323. AddSymbol(o.ref^.symbol.name,false);
  324. asmwrite(o.ref^.symbol.name);
  325. if o.ref^.offset=0 then
  326. exit;
  327. end;
  328. if o.ref^.offset>0 then
  329. asmwrite('+');
  330. asmwrite(tostr(o.ref^.offset));
  331. end;
  332. end;
  333. else
  334. internalerror(10001);
  335. end;
  336. end;
  337. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  338. begin
  339. case o.typ of
  340. top_reg :
  341. AsmWrite(nasm_regname(o.reg));
  342. top_ref :
  343. if o.ref^.refaddr=addr_no then
  344. WriteReference(o.ref^)
  345. else
  346. begin
  347. if not(
  348. (op=A_JCXZ) or (op=A_JECXZ) or
  349. {$ifdef x86_64}
  350. (op=A_JRCXZ) or
  351. {$endif x86_64}
  352. (op=A_LOOP) or (op=A_LOOPE) or
  353. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  354. (op=A_LOOPZ)
  355. ) then
  356. AsmWrite('NEAR ');
  357. AsmWrite(o.ref^.symbol.name);
  358. if SmartAsm then
  359. AddSymbol(o.ref^.symbol.name,false);
  360. if o.ref^.offset>0 then
  361. AsmWrite('+'+tostr(o.ref^.offset))
  362. else
  363. if o.ref^.offset<0 then
  364. AsmWrite(tostr(o.ref^.offset));
  365. end;
  366. top_const :
  367. AsmWrite(tostr(aint(o.val)));
  368. else
  369. internalerror(10001);
  370. end;
  371. end;
  372. const
  373. ait_const2str : array[aitconst_128bit..aitconst_secrel32_symbol] of string[20]=(
  374. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  375. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  376. #9'RVA'#9,#9'SECREL32'#9
  377. );
  378. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  379. const
  380. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  381. '.text',
  382. '.data',
  383. '.data',
  384. '.rodata',
  385. '.bss',
  386. '.tbss',
  387. '.pdata',
  388. '.text','.data','.data','.data','.data',
  389. '.stab',
  390. '.stabstr',
  391. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  392. '.eh_frame',
  393. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  394. '.fpc',
  395. '.extrtti',
  396. '',
  397. '.init',
  398. '.fini',
  399. '.objc_class',
  400. '.objc_meta_class',
  401. '.objc_cat_cls_meth',
  402. '.objc_cat_inst_meth',
  403. '.objc_protocol',
  404. '.objc_string_object',
  405. '.objc_cls_meth',
  406. '.objc_inst_meth',
  407. '.objc_cls_refs',
  408. '.objc_message_refs',
  409. '.objc_symbols',
  410. '.objc_category',
  411. '.objc_class_vars',
  412. '.objc_instance_vars',
  413. '.objc_module_info',
  414. '.objc_class_names',
  415. '.objc_meth_var_types',
  416. '.objc_meth_var_names',
  417. '.objc_selector_strs',
  418. '.objc_protocol_ext',
  419. '.objc_class_ext',
  420. '.objc_property',
  421. '.objc_image_info',
  422. '.objc_cstring_object',
  423. '.objc_sel_fixup',
  424. '__DATA,__objc_data',
  425. '__DATA,__objc_const',
  426. '.objc_superrefs',
  427. '__DATA, __datacoal_nt,coalesced',
  428. '.objc_classlist',
  429. '.objc_nlclasslist',
  430. '.objc_catlist',
  431. '.obcj_nlcatlist',
  432. '.objc_protolist'
  433. );
  434. begin
  435. AsmLn;
  436. AsmWrite('SECTION ');
  437. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  438. Thus, data which normally goes into .rodata and .rodata_norel sections must
  439. end up in .data section }
  440. if (atype in [sec_rodata,sec_rodata_norel]) and
  441. (target_info.system=system_i386_go32v2) then
  442. AsmWrite('.data')
  443. else
  444. AsmWrite(secnames[atype]);
  445. if create_smartlink_sections and
  446. (atype<>sec_bss) and
  447. (aname<>'') then
  448. begin
  449. AsmWrite('.');
  450. AsmWrite(aname);
  451. end;
  452. AsmLn;
  453. LasTSecType:=atype;
  454. end;
  455. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  456. {$ifdef cpuextended}
  457. type
  458. t80bitarray = array[0..9] of byte;
  459. {$endif cpuextended}
  460. var
  461. s : string;
  462. hp : tai;
  463. counter,
  464. lines,
  465. i,j,l : longint;
  466. InlineLevel : longint;
  467. consttype : taiconst_type;
  468. do_line,
  469. quoted : boolean;
  470. co : comp;
  471. sin : single;
  472. d : double;
  473. {$ifdef cpuextended}
  474. e : extended;
  475. {$endif cpuextended}
  476. begin
  477. if not assigned(p) then
  478. exit;
  479. InlineLevel:=0;
  480. { lineinfo is only needed for al_procedures (PFV) }
  481. do_line:=(cs_asm_source in current_settings.globalswitches) or
  482. ((cs_lineinfo in current_settings.moduleswitches)
  483. and (p=current_asmdata.asmlists[al_procedures]));
  484. hp:=tai(p.first);
  485. while assigned(hp) do
  486. begin
  487. prefetch(pointer(hp.next)^);
  488. if not(hp.typ in SkipLineInfo) then
  489. begin
  490. current_filepos:=tailineinfo(hp).fileinfo;
  491. { no line info for inlined code }
  492. if do_line and (inlinelevel=0) then
  493. WriteSourceLine(hp as tailineinfo);
  494. end;
  495. case hp.typ of
  496. ait_comment :
  497. Begin
  498. AsmWrite(target_asm.comment);
  499. AsmWritePChar(tai_comment(hp).str);
  500. AsmLn;
  501. End;
  502. ait_regalloc :
  503. begin
  504. if (cs_asm_regalloc in current_settings.globalswitches) then
  505. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  506. regallocstr[tai_regalloc(hp).ratype]);
  507. end;
  508. ait_tempalloc :
  509. begin
  510. if (cs_asm_tempalloc in current_settings.globalswitches) then
  511. WriteTempalloc(tai_tempalloc(hp));
  512. end;
  513. ait_section :
  514. begin
  515. if tai_section(hp).sectype<>sec_none then
  516. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  517. LasTSecType:=tai_section(hp).sectype;
  518. end;
  519. ait_align :
  520. begin
  521. { nasm gives warnings when it finds align in bss as it
  522. wants to store data }
  523. if (lastsectype<>sec_bss) and
  524. (tai_align(hp).aligntype>1) then
  525. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  526. end;
  527. ait_datablock :
  528. begin
  529. if tai_datablock(hp).is_global then
  530. begin
  531. AsmWrite(#9'GLOBAL ');
  532. AsmWriteLn(tai_datablock(hp).sym.name);
  533. end;
  534. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  535. if SmartAsm then
  536. AddSymbol(tai_datablock(hp).sym.name,true);
  537. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  538. end;
  539. ait_const:
  540. begin
  541. consttype:=tai_const(hp).consttype;
  542. case consttype of
  543. aitconst_64bit :
  544. begin
  545. if assigned(tai_const(hp).sym) then
  546. internalerror(200404292);
  547. AsmWrite(ait_const2str[aitconst_32bit]);
  548. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  549. AsmWrite(',');
  550. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  551. AsmLn;
  552. end;
  553. aitconst_uleb128bit,
  554. aitconst_sleb128bit,
  555. aitconst_128bit:
  556. begin
  557. end;
  558. aitconst_32bit,
  559. aitconst_16bit,
  560. aitconst_8bit,
  561. aitconst_rva_symbol,
  562. aitconst_secrel32_symbol :
  563. begin
  564. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  565. l:=0;
  566. repeat
  567. if assigned(tai_const(hp).sym) then
  568. begin
  569. if SmartAsm then
  570. begin
  571. AddSymbol(tai_const(hp).sym.name,false);
  572. if assigned(tai_const(hp).endsym) then
  573. AddSymbol(tai_const(hp).endsym.name,false);
  574. end;
  575. if assigned(tai_const(hp).endsym) then
  576. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  577. else
  578. s:=tai_const(hp).sym.name;
  579. if tai_const(hp).value<>0 then
  580. s:=s+tostr_with_plus(tai_const(hp).value);
  581. end
  582. else
  583. s:=tostr(tai_const(hp).value);
  584. AsmWrite(s);
  585. inc(l,length(s));
  586. if (l>line_length) or
  587. (hp.next=nil) or
  588. (tai(hp.next).typ<>ait_const) or
  589. (tai_const(hp.next).consttype<>consttype) then
  590. break;
  591. hp:=tai(hp.next);
  592. AsmWrite(',');
  593. until false;
  594. AsmLn;
  595. end;
  596. else
  597. internalerror(200704252);
  598. end;
  599. end;
  600. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  601. ait_real_80bit :
  602. begin
  603. if do_line then
  604. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  605. { Make sure e is a extended type, bestreal could be
  606. a different type (bestreal) !! (PFV) }
  607. e:=tai_real_80bit(hp).value;
  608. AsmWrite(#9#9'DB'#9);
  609. for i:=0 to 9 do
  610. begin
  611. if i<>0 then
  612. AsmWrite(',');
  613. AsmWrite(tostr(t80bitarray(e)[i]));
  614. end;
  615. for i:=11 to tai_real_80bit(hp).savesize do
  616. AsmWrite(',0');
  617. AsmLn;
  618. end;
  619. {$else cpuextended}
  620. ait_real_80bit :
  621. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  622. {$endif cpuextended}
  623. // ait_real_64bit :
  624. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  625. ait_real_64bit :
  626. begin
  627. if do_line then
  628. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  629. d:=tai_real_64bit(hp).value;
  630. { swap the values to correct endian if required }
  631. if source_info.endian <> target_info.endian then
  632. swap64bitarray(t64bitarray(d));
  633. AsmWrite(#9#9'DB'#9);
  634. {$ifdef arm}
  635. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  636. begin
  637. for i:=4 to 7 do
  638. begin
  639. if i<>4 then
  640. AsmWrite(',');
  641. AsmWrite(tostr(t64bitarray(d)[i]));
  642. end;
  643. for i:=0 to 3 do
  644. begin
  645. AsmWrite(',');
  646. AsmWrite(tostr(t64bitarray(d)[i]));
  647. end;
  648. end
  649. else
  650. {$endif arm}
  651. begin
  652. for i:=0 to 7 do
  653. begin
  654. if i<>0 then
  655. AsmWrite(',');
  656. AsmWrite(tostr(t64bitarray(d)[i]));
  657. end;
  658. end;
  659. AsmLn;
  660. end;
  661. // ait_real_32bit :
  662. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  663. ait_real_32bit :
  664. begin
  665. if do_line then
  666. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  667. sin:=tai_real_32bit(hp).value;
  668. { swap the values to correct endian if required }
  669. if source_info.endian <> target_info.endian then
  670. swap32bitarray(t32bitarray(sin));
  671. AsmWrite(#9#9'DB'#9);
  672. for i:=0 to 3 do
  673. begin
  674. if i<>0 then
  675. AsmWrite(',');
  676. AsmWrite(tostr(t32bitarray(sin)[i]));
  677. end;
  678. AsmLn;
  679. end;
  680. // ait_comp_64bit :
  681. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  682. ait_comp_64bit :
  683. begin
  684. if do_line then
  685. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  686. AsmWrite(#9#9'DB'#9);
  687. co:=comp(tai_comp_64bit(hp).value);
  688. { swap the values to correct endian if required }
  689. if source_info.endian <> target_info.endian then
  690. swap64bitarray(t64bitarray(co));
  691. for i:=0 to 7 do
  692. begin
  693. if i<>0 then
  694. AsmWrite(',');
  695. AsmWrite(tostr(t64bitarray(co)[i]));
  696. end;
  697. AsmLn;
  698. end;
  699. ait_string :
  700. begin
  701. counter := 0;
  702. lines := tai_string(hp).len div line_length;
  703. { separate lines in different parts }
  704. if tai_string(hp).len > 0 then
  705. Begin
  706. for j := 0 to lines-1 do
  707. begin
  708. AsmWrite(#9#9'DB'#9);
  709. quoted:=false;
  710. for i:=counter to counter+line_length-1 do
  711. begin
  712. { it is an ascii character. }
  713. if (ord(tai_string(hp).str[i])>31) and
  714. (ord(tai_string(hp).str[i])<128) and
  715. (tai_string(hp).str[i]<>'"') then
  716. begin
  717. if not(quoted) then
  718. begin
  719. if i>counter then
  720. AsmWrite(',');
  721. AsmWrite('"');
  722. end;
  723. AsmWrite(tai_string(hp).str[i]);
  724. quoted:=true;
  725. end { if > 31 and < 128 and ord('"') }
  726. else
  727. begin
  728. if quoted then
  729. AsmWrite('"');
  730. if i>counter then
  731. AsmWrite(',');
  732. quoted:=false;
  733. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  734. end;
  735. end; { end for i:=0 to... }
  736. if quoted then AsmWrite('"');
  737. AsmWrite(target_info.newline);
  738. inc(counter,line_length);
  739. end; { end for j:=0 ... }
  740. { do last line of lines }
  741. if counter<tai_string(hp).len then
  742. AsmWrite(#9#9'DB'#9);
  743. quoted:=false;
  744. for i:=counter to tai_string(hp).len-1 do
  745. begin
  746. { it is an ascii character. }
  747. if (ord(tai_string(hp).str[i])>31) and
  748. (ord(tai_string(hp).str[i])<128) and
  749. (tai_string(hp).str[i]<>'"') then
  750. begin
  751. if not(quoted) then
  752. begin
  753. if i>counter then
  754. AsmWrite(',');
  755. AsmWrite('"');
  756. end;
  757. AsmWrite(tai_string(hp).str[i]);
  758. quoted:=true;
  759. end { if > 31 and < 128 and " }
  760. else
  761. begin
  762. if quoted then
  763. AsmWrite('"');
  764. if i>counter then
  765. AsmWrite(',');
  766. quoted:=false;
  767. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  768. end;
  769. end; { end for i:=0 to... }
  770. if quoted then
  771. AsmWrite('"');
  772. end;
  773. AsmLn;
  774. end;
  775. ait_label :
  776. begin
  777. if tai_label(hp).labsym.is_used then
  778. AsmWriteLn(tai_label(hp).labsym.name+':');
  779. if SmartAsm then
  780. AddSymbol(tai_label(hp).labsym.name,true);
  781. end;
  782. ait_symbol :
  783. begin
  784. if tai_symbol(hp).has_value then
  785. internalerror(2009090803);
  786. if tai_symbol(hp).is_global then
  787. begin
  788. AsmWrite(#9'GLOBAL ');
  789. AsmWriteLn(tai_symbol(hp).sym.name);
  790. end;
  791. AsmWrite(tai_symbol(hp).sym.name);
  792. if SmartAsm then
  793. AddSymbol(tai_symbol(hp).sym.name,true);
  794. if assigned(hp.next) and not(tai(hp.next).typ in
  795. [ait_const,
  796. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  797. AsmWriteLn(':')
  798. end;
  799. ait_symbol_end : ;
  800. ait_instruction :
  801. begin
  802. taicpu(hp).CheckNonCommutativeOpcodes;
  803. { We need intel order, no At&t }
  804. taicpu(hp).SetOperandOrder(op_intel);
  805. s:='';
  806. if ((taicpu(hp).opcode=A_FADDP) or
  807. (taicpu(hp).opcode=A_FMULP))
  808. and (taicpu(hp).ops=0) then
  809. begin
  810. taicpu(hp).allocate_oper(2);
  811. taicpu(hp).oper[0]^.typ:=top_reg;
  812. taicpu(hp).oper[0]^.reg:=NR_ST1;
  813. taicpu(hp).oper[1]^.typ:=top_reg;
  814. taicpu(hp).oper[1]^.reg:=NR_ST;
  815. end;
  816. if taicpu(hp).opcode=A_FWAIT then
  817. AsmWriteln(#9#9'DB'#9'09bh')
  818. else
  819. begin
  820. { We need to explicitely set
  821. word prefix to get selectors
  822. to be pushed in 2 bytes PM }
  823. if (taicpu(hp).opsize=S_W) and
  824. ((taicpu(hp).opcode=A_PUSH) or
  825. (taicpu(hp).opcode=A_POP)) and
  826. (taicpu(hp).oper[0]^.typ=top_reg) and
  827. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  828. AsmWriteln(#9#9'DB'#9'066h');
  829. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  830. if taicpu(hp).ops<>0 then
  831. begin
  832. if is_calljmp(taicpu(hp).opcode) then
  833. begin
  834. AsmWrite(#9);
  835. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  836. end
  837. else
  838. begin
  839. for i:=0 to taicpu(hp).ops-1 do
  840. begin
  841. if i=0 then
  842. AsmWrite(#9)
  843. else
  844. AsmWrite(',');
  845. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  846. end;
  847. end;
  848. end;
  849. AsmLn;
  850. end;
  851. end;
  852. ait_stab,
  853. ait_force_line,
  854. ait_function_name : ;
  855. ait_cutobject :
  856. begin
  857. if SmartAsm then
  858. begin
  859. { only reset buffer if nothing has changed }
  860. if AsmSize=AsmStartSize then
  861. AsmClear
  862. else
  863. begin
  864. if SmartAsm then
  865. begin
  866. WriteSmartExternals;
  867. FreeExternChainList;
  868. end;
  869. AsmClose;
  870. DoAssemble;
  871. AsmCreate(tai_cutobject(hp).place);
  872. end;
  873. { avoid empty files }
  874. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  875. begin
  876. if tai(hp.next).typ=ait_section then
  877. lasTSectype:=tai_section(hp.next).sectype;
  878. hp:=tai(hp.next);
  879. end;
  880. if lasTSectype<>sec_none then
  881. WriteSection(lasTSectype,'');
  882. AsmStartSize:=AsmSize;
  883. end;
  884. end;
  885. ait_marker :
  886. if tai_marker(hp).kind=mark_NoLineInfoStart then
  887. inc(InlineLevel)
  888. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  889. dec(InlineLevel);
  890. ait_directive :
  891. begin
  892. case tai_directive(hp).directive of
  893. asd_nasm_import :
  894. AsmWrite('import ');
  895. asd_extern :
  896. AsmWrite('EXTERN ');
  897. else
  898. internalerror(200509191);
  899. end;
  900. if tai_directive(hp).name<>'' then
  901. begin
  902. if SmartAsm then
  903. AddSymbol(tai_directive(hp).name,false);
  904. AsmWrite(tai_directive(hp).name);
  905. end;
  906. AsmLn;
  907. end;
  908. ait_seh_directive :
  909. { Ignore for now };
  910. else
  911. internalerror(10000);
  912. end;
  913. hp:=tai(hp.next);
  914. end;
  915. end;
  916. procedure T386NasmAssembler.WriteExternals;
  917. var
  918. sym : TAsmSymbol;
  919. i : longint;
  920. begin
  921. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  922. begin
  923. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  924. if sym.bind=AB_EXTERNAL then
  925. AsmWriteln('EXTERN'#9+sym.name);
  926. end;
  927. end;
  928. procedure T386NasmAssembler.WriteSmartExternals;
  929. var
  930. EC : PExternChain;
  931. begin
  932. EC:=FEC;
  933. while assigned(EC) do
  934. begin
  935. if not EC^.is_defined then
  936. AsmWriteln('EXTERN'#9+EC^.psym^);
  937. EC:=EC^.next;
  938. end;
  939. end;
  940. procedure T386NasmAssembler.WriteAsmList;
  941. var
  942. hal : tasmlisttype;
  943. begin
  944. {$ifdef EXTDEBUG}
  945. if current_module.mainsource<>'' then
  946. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  947. {$endif}
  948. AsmWriteLn('BITS 32');
  949. AsmLn;
  950. WriteExternals;
  951. for hal:=low(TasmlistType) to high(TasmlistType) do
  952. begin
  953. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  954. writetree(current_asmdata.asmlists[hal]);
  955. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  956. end;
  957. AsmLn;
  958. if SmartAsm then
  959. begin
  960. WriteSmartExternals;
  961. FreeExternChainList;
  962. end;
  963. {$ifdef EXTDEBUG}
  964. if current_module.mainsource<>'' then
  965. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  966. {$endif EXTDEBUG}
  967. end;
  968. {*****************************************************************************
  969. Initialize
  970. *****************************************************************************}
  971. const
  972. as_i386_nasmcoff_info : tasminfo =
  973. (
  974. id : as_i386_nasmcoff;
  975. idtxt : 'NASMCOFF';
  976. asmbin : 'nasm';
  977. asmcmd : '-f coff -o $OBJ $ASM';
  978. supported_targets : [system_i386_go32v2];
  979. flags : [af_allowdirect,af_needar,af_no_debug];
  980. labelprefix : '..@';
  981. comment : '; ';
  982. dollarsign: '$';
  983. );
  984. as_i386_nasmwin32_info : tasminfo =
  985. (
  986. id : as_i386_nasmwin32;
  987. idtxt : 'NASMWIN32';
  988. asmbin : 'nasm';
  989. asmcmd : '-f win32 -o $OBJ $ASM';
  990. supported_targets : [system_i386_win32];
  991. flags : [af_allowdirect,af_needar,af_no_debug];
  992. labelprefix : '..@';
  993. comment : '; ';
  994. dollarsign: '$';
  995. );
  996. as_i386_nasmobj_info : tasminfo =
  997. (
  998. id : as_i386_nasmobj;
  999. idtxt : 'NASMOBJ';
  1000. asmbin : 'nasm';
  1001. asmcmd : '-f obj -o $OBJ $ASM';
  1002. supported_targets : [system_i386_embedded];
  1003. flags : [af_allowdirect,af_needar,af_no_debug];
  1004. labelprefix : '..@';
  1005. comment : '; ';
  1006. dollarsign: '$';
  1007. );
  1008. as_i386_nasmwdosx_info : tasminfo =
  1009. (
  1010. id : as_i386_nasmwdosx;
  1011. idtxt : 'NASMWDOSX';
  1012. asmbin : 'nasm';
  1013. asmcmd : '-f win32 -o $OBJ $ASM';
  1014. supported_targets : [system_i386_wdosx];
  1015. flags : [af_allowdirect,af_needar,af_no_debug];
  1016. labelprefix : '..@';
  1017. comment : '; ';
  1018. dollarsign: '$';
  1019. );
  1020. as_i386_nasmelf_info : tasminfo =
  1021. (
  1022. id : as_i386_nasmelf;
  1023. idtxt : 'NASMELF';
  1024. asmbin : 'nasm';
  1025. asmcmd : '-f elf -o $OBJ $ASM';
  1026. supported_targets : [system_i386_linux];
  1027. flags : [af_allowdirect,af_needar,af_no_debug];
  1028. labelprefix : '..@';
  1029. comment : '; ';
  1030. dollarsign: '$';
  1031. );
  1032. as_i386_nasmbeos_info : tasminfo =
  1033. (
  1034. id : as_i386_nasmbeos;
  1035. idtxt : 'NASMELF';
  1036. asmbin : 'nasm';
  1037. asmcmd : '-f elf -o $OBJ $ASM';
  1038. supported_targets : [system_i386_beos];
  1039. flags : [af_allowdirect,af_needar,af_no_debug];
  1040. labelprefix : '..@';
  1041. comment : '; ';
  1042. dollarsign: '$';
  1043. );
  1044. as_i386_nasmhaiku_info : tasminfo =
  1045. (
  1046. id : as_i386_nasmhaiku;
  1047. idtxt : 'NASMELF';
  1048. asmbin : 'nasm';
  1049. asmcmd : '-f elf -o $OBJ $ASM';
  1050. supported_targets : [system_i386_haiku];
  1051. flags : [af_allowdirect,af_needar,af_no_debug];
  1052. labelprefix : '..@';
  1053. comment : '; ';
  1054. dollarsign: '$';
  1055. );
  1056. initialization
  1057. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1058. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1059. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1060. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1061. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1062. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1063. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1064. end.