ag386nsm.pas 37 KB

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