ag386nsm.pas 35 KB

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