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