agx86nsm.pas 37 KB

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