ag386nsm.pas 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit implements an asmoutput class for the Nasm assembler with
  5. Intel syntax for the i386+
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ag386nsm;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses aasm,assemble;
  23. type
  24. T386NasmAssembler = class(texternalassembler)
  25. procedure WriteTree(p:taasmoutput);override;
  26. procedure WriteAsmList;override;
  27. procedure WriteExternals;
  28. end;
  29. implementation
  30. uses
  31. {$ifdef delphi}
  32. sysutils,
  33. {$endif}
  34. cutils,globtype,globals,systems,cclasses,
  35. fmodule,finput,verbose,cpubase,cpuasm,tainst
  36. ;
  37. const
  38. line_length = 64;
  39. int_nasmreg2str : reg2strtable = ('',
  40. 'eax','ecx','edx','ebx','esp','ebp','esi','edi',
  41. 'ax','cx','dx','bx','sp','bp','si','di',
  42. 'al','cl','dl','bl','ah','ch','bh','dh',
  43. 'cs','ds','es','ss','fs','gs',
  44. 'st0','st0','st1','st2','st3','st4','st5','st6','st7',
  45. 'dr0','dr1','dr2','dr3','dr6','dr7',
  46. 'cr0','cr2','cr3','cr4',
  47. 'tr3','tr4','tr5','tr6','tr7',
  48. 'mm0','mm1','mm2','mm3','mm4','mm5','mm6','mm7',
  49. 'xmm0','xmm1','xmm2','xmm3','xmm4','xmm5','xmm6','xmm7'
  50. );
  51. var
  52. lastfileinfo : tfileposinfo;
  53. infile,
  54. lastinfile : tinputfile;
  55. function fixline(s:string):string;
  56. {
  57. return s with all leading and ending spaces and tabs removed
  58. }
  59. var
  60. i,j,k : longint;
  61. begin
  62. i:=length(s);
  63. while (i>0) and (s[i] in [#9,' ']) do
  64. dec(i);
  65. j:=1;
  66. while (j<i) and (s[j] in [#9,' ']) do
  67. inc(j);
  68. for k:=j to i do
  69. if s[k] in [#0..#31,#127..#255] then
  70. s[k]:='.';
  71. fixline:=Copy(s,j,i-j+1);
  72. end;
  73. function single2str(d : single) : string;
  74. var
  75. hs : string;
  76. p : byte;
  77. begin
  78. str(d,hs);
  79. { nasm expects a lowercase e }
  80. p:=pos('E',hs);
  81. if p>0 then
  82. hs[p]:='e';
  83. p:=pos('+',hs);
  84. if p>0 then
  85. delete(hs,p,1);
  86. single2str:=lower(hs);
  87. end;
  88. function double2str(d : double) : string;
  89. var
  90. hs : string;
  91. p : byte;
  92. begin
  93. str(d,hs);
  94. { nasm expects a lowercase e }
  95. p:=pos('E',hs);
  96. if p>0 then
  97. hs[p]:='e';
  98. p:=pos('+',hs);
  99. if p>0 then
  100. delete(hs,p,1);
  101. double2str:=lower(hs);
  102. end;
  103. function extended2str(e : extended) : string;
  104. var
  105. hs : string;
  106. p : byte;
  107. begin
  108. str(e,hs);
  109. { nasm expects a lowercase e }
  110. p:=pos('E',hs);
  111. if p>0 then
  112. hs[p]:='e';
  113. p:=pos('+',hs);
  114. if p>0 then
  115. delete(hs,p,1);
  116. extended2str:=lower(hs);
  117. end;
  118. function comp2str(d : bestreal) : string;
  119. type
  120. pdouble = ^double;
  121. var
  122. c : comp;
  123. dd : pdouble;
  124. begin
  125. {$ifdef FPC}
  126. c:=comp(d);
  127. {$else}
  128. c:=d;
  129. {$endif}
  130. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  131. comp2str:=double2str(dd^);
  132. end;
  133. function getreferencestring(var ref : treference) : string;
  134. var
  135. s : string;
  136. first : boolean;
  137. begin
  138. with ref do
  139. begin
  140. first:=true;
  141. inc(offset,offsetfixup);
  142. offsetfixup:=0;
  143. if ref.segment<>R_NO then
  144. s:='['+std_reg2str[segment]+':'
  145. else
  146. s:='[';
  147. if assigned(symbol) then
  148. begin
  149. s:=s+symbol.name;
  150. first:=false;
  151. end;
  152. if (base<>R_NO) then
  153. begin
  154. if not(first) then
  155. s:=s+'+'
  156. else
  157. first:=false;
  158. s:=s+std_reg2str[base];
  159. end;
  160. if (index<>R_NO) then
  161. begin
  162. if not(first) then
  163. s:=s+'+'
  164. else
  165. first:=false;
  166. s:=s+std_reg2str[index];
  167. if scalefactor<>0 then
  168. s:=s+'*'+tostr(scalefactor);
  169. end;
  170. if offset<0 then
  171. s:=s+tostr(offset)
  172. else if (offset>0) then
  173. s:=s+'+'+tostr(offset);
  174. if s[length(s)]='[' then
  175. s:=s+'0';
  176. s:=s+']';
  177. end;
  178. getreferencestring:=s;
  179. end;
  180. function sizestr(s:topsize;dest:boolean):string;
  181. begin
  182. case s of
  183. S_B : sizestr:='byte ';
  184. S_W : sizestr:='word ';
  185. S_L : sizestr:='dword ';
  186. S_IS : sizestr:='word ';
  187. S_IL : sizestr:='dword ';
  188. S_IQ : sizestr:='qword ';
  189. S_FS : sizestr:='dword ';
  190. S_FL : sizestr:='qword ';
  191. S_FX : sizestr:='tword ';
  192. S_BW : if dest then
  193. sizestr:='word '
  194. else
  195. sizestr:='byte ';
  196. S_BL : if dest then
  197. sizestr:='dword '
  198. else
  199. sizestr:='byte ';
  200. S_WL : if dest then
  201. sizestr:='dword '
  202. else
  203. sizestr:='word ';
  204. else { S_NO }
  205. sizestr:='';
  206. end;
  207. end;
  208. function getopstr(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean) : string;
  209. var
  210. hs : string;
  211. begin
  212. case o.typ of
  213. top_reg :
  214. getopstr:=int_nasmreg2str[o.reg];
  215. top_const :
  216. begin
  217. if (ops=1) and (opcode<>A_RET) then
  218. getopstr:=sizestr(s,dest)+tostr(longint(o.val))
  219. else
  220. getopstr:=tostr(longint(o.val));
  221. end;
  222. top_symbol :
  223. begin
  224. if assigned(o.sym) then
  225. hs:='dword '+o.sym.name
  226. else
  227. hs:='dword ';
  228. if o.symofs>0 then
  229. hs:=hs+'+'+tostr(o.symofs)
  230. else
  231. if o.symofs<0 then
  232. hs:=hs+tostr(o.symofs)
  233. else
  234. if not(assigned(o.sym)) then
  235. hs:=hs+'0';
  236. getopstr:=hs;
  237. end;
  238. top_ref :
  239. begin
  240. hs:=getreferencestring(o.ref^);
  241. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  242. (opcode = A_LSS) or (opcode = A_LFS) or
  243. (opcode = A_LES) or (opcode = A_LDS) or
  244. (opcode = A_SHR) or (opcode = A_SHL) or
  245. (opcode = A_SAR) or (opcode = A_SAL) or
  246. (opcode = A_OUT) or (opcode = A_IN)) then
  247. begin
  248. hs:=sizestr(s,dest)+hs;
  249. end;
  250. getopstr:=hs;
  251. end;
  252. else
  253. internalerror(10001);
  254. end;
  255. end;
  256. function getopstr_jmp(const o:toper; op : tasmop) : string;
  257. var
  258. hs : string;
  259. begin
  260. case o.typ of
  261. top_reg :
  262. getopstr_jmp:=int_nasmreg2str[o.reg];
  263. top_ref :
  264. getopstr_jmp:=getreferencestring(o.ref^);
  265. top_const :
  266. getopstr_jmp:=tostr(longint(o.val));
  267. top_symbol :
  268. begin
  269. hs:=o.sym.name;
  270. if o.symofs>0 then
  271. hs:=hs+'+'+tostr(o.symofs)
  272. else
  273. if o.symofs<0 then
  274. hs:=hs+tostr(o.symofs);
  275. if (op=A_JCXZ) or (op=A_JECXZ) or
  276. (op=A_LOOP) or (op=A_LOOPE) or
  277. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  278. (op=A_LOOPZ) then
  279. getopstr_jmp:=hs
  280. else
  281. getopstr_jmp:='NEAR '+hs;
  282. end;
  283. else
  284. internalerror(10001);
  285. end;
  286. end;
  287. {****************************************************************************
  288. T386NasmAssembler
  289. ****************************************************************************}
  290. var
  291. LastSec : tsection;
  292. const
  293. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  294. (#9'DD'#9,#9'DW'#9,#9'DB'#9);
  295. Function PadTabs(const p:string;addch:char):string;
  296. var
  297. s : string;
  298. i : longint;
  299. begin
  300. i:=length(p);
  301. if addch<>#0 then
  302. begin
  303. inc(i);
  304. s:=p+addch;
  305. end
  306. else
  307. s:=p;
  308. if i<8 then
  309. PadTabs:=s+#9#9
  310. else
  311. PadTabs:=s+#9;
  312. end;
  313. procedure T386NasmAssembler.WriteTree(p:taasmoutput);
  314. const
  315. allocstr : array[boolean] of string[10]=(' released',' allocated');
  316. nolinetai =[ait_label,
  317. ait_regalloc,ait_tempalloc,
  318. ait_stabn,ait_stabs,ait_section,
  319. ait_cut,ait_marker,ait_align,ait_stab_function_name];
  320. var
  321. s : string;
  322. {prefix,
  323. suffix : string; no need here }
  324. hp : tai;
  325. counter,
  326. lines,
  327. i,j,l : longint;
  328. InlineLevel : longint;
  329. consttyp : tait;
  330. found,
  331. do_line,
  332. quoted : boolean;
  333. sep : char;
  334. begin
  335. if not assigned(p) then
  336. exit;
  337. InlineLevel:=0;
  338. { lineinfo is only needed for codesegment (PFV) }
  339. do_line:=(cs_asm_source in aktglobalswitches) or
  340. ((cs_lineinfo in aktmoduleswitches)
  341. and (p=codesegment));
  342. hp:=tai(p.first);
  343. while assigned(hp) do
  344. begin
  345. aktfilepos:=hp.fileinfo;
  346. if not(hp.typ in nolinetai) then
  347. begin
  348. if do_line then
  349. begin
  350. { load infile }
  351. if lastfileinfo.fileindex<>hp.fileinfo.fileindex then
  352. begin
  353. infile:=current_module.sourcefiles.get_file(hp.fileinfo.fileindex);
  354. if assigned(infile) then
  355. begin
  356. { open only if needed !! }
  357. if (cs_asm_source in aktglobalswitches) then
  358. infile.open;
  359. end;
  360. { avoid unnecessary reopens of the same file !! }
  361. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  362. { be sure to change line !! }
  363. lastfileinfo.line:=-1;
  364. end;
  365. { write source }
  366. if (cs_asm_source in aktglobalswitches) and
  367. assigned(infile) then
  368. begin
  369. if (infile<>lastinfile) then
  370. begin
  371. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  372. if assigned(lastinfile) then
  373. lastinfile.close;
  374. end;
  375. if (hp.fileinfo.line<>lastfileinfo.line) and
  376. ((hp.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  377. begin
  378. if (hp.fileinfo.line<>0) and
  379. ((infile.linebuf^[hp.fileinfo.line]>=0) or (InlineLevel>0)) then
  380. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  381. fixline(infile.GetLineStr(hp.fileinfo.line)));
  382. { set it to a negative value !
  383. to make that is has been read already !! PM }
  384. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  385. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  386. end;
  387. end;
  388. lastfileinfo:=hp.fileinfo;
  389. lastinfile:=infile;
  390. end;
  391. end;
  392. case hp.typ of
  393. ait_comment :
  394. Begin
  395. AsmWrite(target_asm.comment);
  396. AsmWritePChar(tai_asm_comment(hp).str);
  397. AsmLn;
  398. End;
  399. ait_regalloc :
  400. begin
  401. if (cs_asm_regalloc in aktglobalswitches) then
  402. AsmWriteLn(target_asm.comment+'Register '+std_reg2str[tairegalloc(hp).reg]+
  403. allocstr[tairegalloc(hp).allocation]);
  404. end;
  405. ait_tempalloc :
  406. begin
  407. if (cs_asm_tempalloc in aktglobalswitches) then
  408. AsmWriteLn(target_asm.comment+'Temp '+tostr(taitempalloc(hp).temppos)+','+
  409. tostr(taitempalloc(hp).tempsize)+allocstr[taitempalloc(hp).allocation]);
  410. end;
  411. ait_section :
  412. begin
  413. if tai_section(hp).sec<>sec_none then
  414. begin
  415. AsmLn;
  416. AsmWriteLn('SECTION '+target_asm.secnames[tai_section(hp).sec]);
  417. end;
  418. LastSec:=tai_section(hp).sec;
  419. end;
  420. ait_align :
  421. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  422. ait_datablock :
  423. begin
  424. if tai_datablock(hp).is_global then
  425. begin
  426. AsmWrite(#9'GLOBAL ');
  427. AsmWriteLn(tai_datablock(hp).sym.name);
  428. end;
  429. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  430. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  431. end;
  432. ait_const_32bit,
  433. ait_const_16bit,
  434. ait_const_8bit :
  435. begin
  436. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  437. consttyp:=hp.typ;
  438. l:=0;
  439. repeat
  440. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  441. if found then
  442. begin
  443. hp:=tai(hp.next);
  444. s:=','+tostr(tai_const(hp).value);
  445. AsmWrite(s);
  446. inc(l,length(s));
  447. end;
  448. until (not found) or (l>line_length);
  449. AsmLn;
  450. end;
  451. ait_const_symbol :
  452. begin
  453. AsmWrite(#9#9'DD'#9);
  454. AsmWrite(tai_const_symbol(hp).sym.name);
  455. if tai_const_symbol(hp).offset>0 then
  456. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  457. else if tai_const_symbol(hp).offset<0 then
  458. AsmWrite(tostr(tai_const_symbol(hp).offset));
  459. AsmLn;
  460. end;
  461. ait_const_rva :
  462. begin
  463. AsmWrite(#9#9'RVA'#9);
  464. AsmWriteLn(tai_const_symbol(hp).sym.name);
  465. end;
  466. ait_real_32bit :
  467. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  468. ait_real_64bit :
  469. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  470. ait_real_80bit :
  471. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  472. ait_comp_64bit :
  473. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  474. ait_string :
  475. begin
  476. counter := 0;
  477. lines := tai_string(hp).len div line_length;
  478. { separate lines in different parts }
  479. if tai_string(hp).len > 0 then
  480. Begin
  481. for j := 0 to lines-1 do
  482. begin
  483. AsmWrite(#9#9'DB'#9);
  484. quoted:=false;
  485. for i:=counter to counter+line_length-1 do
  486. begin
  487. { it is an ascii character. }
  488. if (ord(tai_string(hp).str[i])>31) and
  489. (ord(tai_string(hp).str[i])<128) and
  490. (tai_string(hp).str[i]<>'"') then
  491. begin
  492. if not(quoted) then
  493. begin
  494. if i>counter then
  495. AsmWrite(',');
  496. AsmWrite('"');
  497. end;
  498. AsmWrite(tai_string(hp).str[i]);
  499. quoted:=true;
  500. end { if > 31 and < 128 and ord('"') }
  501. else
  502. begin
  503. if quoted then
  504. AsmWrite('"');
  505. if i>counter then
  506. AsmWrite(',');
  507. quoted:=false;
  508. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  509. end;
  510. end; { end for i:=0 to... }
  511. if quoted then AsmWrite('"');
  512. AsmWrite(target_info.newline);
  513. inc(counter,line_length);
  514. end; { end for j:=0 ... }
  515. { do last line of lines }
  516. if counter<tai_string(hp).len then
  517. AsmWrite(#9#9'DB'#9);
  518. quoted:=false;
  519. for i:=counter to tai_string(hp).len-1 do
  520. begin
  521. { it is an ascii character. }
  522. if (ord(tai_string(hp).str[i])>31) and
  523. (ord(tai_string(hp).str[i])<128) and
  524. (tai_string(hp).str[i]<>'"') then
  525. begin
  526. if not(quoted) then
  527. begin
  528. if i>counter then
  529. AsmWrite(',');
  530. AsmWrite('"');
  531. end;
  532. AsmWrite(tai_string(hp).str[i]);
  533. quoted:=true;
  534. end { if > 31 and < 128 and " }
  535. else
  536. begin
  537. if quoted then
  538. AsmWrite('"');
  539. if i>counter then
  540. AsmWrite(',');
  541. quoted:=false;
  542. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  543. end;
  544. end; { end for i:=0 to... }
  545. if quoted then
  546. AsmWrite('"');
  547. end;
  548. AsmLn;
  549. end;
  550. ait_label :
  551. begin
  552. if tai_label(hp).l.is_used then
  553. AsmWriteLn(tai_label(hp).l.name+':');
  554. end;
  555. ait_direct :
  556. begin
  557. AsmWritePChar(tai_direct(hp).str);
  558. AsmLn;
  559. end;
  560. ait_symbol :
  561. begin
  562. if tai_symbol(hp).is_global then
  563. begin
  564. AsmWrite(#9'GLOBAL ');
  565. AsmWriteLn(tai_symbol(hp).sym.name);
  566. end;
  567. AsmWrite(tai_symbol(hp).sym.name);
  568. if assigned(hp.next) and not(tai(hp.next).typ in
  569. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  570. ait_const_symbol,ait_const_rva,
  571. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  572. AsmWriteLn(':')
  573. end;
  574. ait_symbol_end : ;
  575. ait_instruction :
  576. begin
  577. { Must be done with args in ATT order }
  578. taicpu(hp).SetOperandOrder(op_att);
  579. taicpu(hp).CheckNonCommutativeOpcodes;
  580. { We need intel order, no At&t }
  581. taicpu(hp).SetOperandOrder(op_intel);
  582. { Reset
  583. suffix:='';
  584. prefix:='';}
  585. s:='';
  586. if ((taicpu(hp).opcode=A_FADDP) or
  587. (taicpu(hp).opcode=A_FMULP))
  588. and (taicpu(hp).ops=0) then
  589. begin
  590. taicpu(hp).ops:=2;
  591. taicpu(hp).oper[0].typ:=top_reg;
  592. taicpu(hp).oper[0].reg:=R_ST1;
  593. taicpu(hp).oper[1].typ:=top_reg;
  594. taicpu(hp).oper[1].reg:=R_ST;
  595. end;
  596. if taicpu(hp).ops<>0 then
  597. begin
  598. if is_calljmp(taicpu(hp).opcode) then
  599. s:=#9+getopstr_jmp(taicpu(hp).oper[0],taicpu(hp).opcode)
  600. else
  601. begin
  602. { We need to explicitely set
  603. word prefix to get selectors
  604. to be pushed in 2 bytes PM }
  605. if (taicpu(hp).opsize=S_W) and
  606. ((taicpu(hp).opcode=A_PUSH) or
  607. (taicpu(hp).opcode=A_POP)) and
  608. (taicpu(hp).oper[0].typ=top_reg) and
  609. ((taicpu(hp).oper[0].reg>=firstsreg) and
  610. (taicpu(hp).oper[0].reg<=lastsreg)) then
  611. AsmWriteln(#9#9'DB'#9'066h');
  612. for i:=0 to taicpu(hp).ops-1 do
  613. begin
  614. if i=0 then
  615. sep:=#9
  616. else
  617. sep:=',';
  618. s:=s+sep+getopstr(taicpu(hp).oper[i],taicpu(hp).opsize,taicpu(hp).opcode,
  619. taicpu(hp).ops,(i=2));
  620. end;
  621. end;
  622. end;
  623. if taicpu(hp).opcode=A_FWAIT then
  624. AsmWriteln(#9#9'DB'#9'09bh')
  625. else
  626. AsmWriteLn(#9#9+{prefix+}std_op2str[taicpu(hp).opcode]+
  627. cond2str[taicpu(hp).condition]+{suffix+}s);
  628. end;
  629. {$ifdef GDB}
  630. ait_stabn,
  631. ait_stabs,
  632. ait_force_line,
  633. ait_stab_function_name : ;
  634. {$endif GDB}
  635. ait_cut :
  636. begin
  637. { only reset buffer if nothing has changed }
  638. if AsmSize=AsmStartSize then
  639. AsmClear
  640. else
  641. begin
  642. AsmClose;
  643. DoAssemble;
  644. AsmCreate(tai_cut(hp).place);
  645. end;
  646. { avoid empty files }
  647. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  648. begin
  649. if tai(hp.next).typ=ait_section then
  650. lastsec:=tai_section(hp.next).sec;
  651. hp:=tai(hp.next);
  652. end;
  653. if lastsec<>sec_none then
  654. AsmWriteLn('SECTION '+target_asm.secnames[lastsec]);
  655. AsmStartSize:=AsmSize;
  656. end;
  657. ait_marker :
  658. if tai_marker(hp).kind=InlineStart then
  659. inc(InlineLevel)
  660. else if tai_marker(hp).kind=InlineEnd then
  661. dec(InlineLevel);
  662. else
  663. internalerror(10000);
  664. end;
  665. hp:=tai(hp.next);
  666. end;
  667. end;
  668. var
  669. currentasmlist : TExternalAssembler;
  670. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  671. begin
  672. if tasmsymbol(p).defbind=AB_EXTERNAL then
  673. currentasmlist.AsmWriteln('EXTERN'#9+p.name);
  674. end;
  675. procedure T386NasmAssembler.WriteExternals;
  676. begin
  677. currentasmlist:=self;
  678. AsmSymbolList.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  679. end;
  680. procedure T386NasmAssembler.WriteAsmList;
  681. begin
  682. {$ifdef EXTDEBUG}
  683. if assigned(current_module.mainsource) then
  684. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  685. {$endif}
  686. LastSec:=sec_none;
  687. AsmWriteLn('BITS 32');
  688. AsmLn;
  689. countlabelref:=false;
  690. lastfileinfo.line:=-1;
  691. lastfileinfo.fileindex:=0;
  692. lastinfile:=nil;
  693. WriteExternals;
  694. { Nasm doesn't support stabs
  695. WriteTree(debuglist);}
  696. WriteTree(codesegment);
  697. WriteTree(datasegment);
  698. WriteTree(consts);
  699. WriteTree(rttilist);
  700. WriteTree(resourcestringlist);
  701. WriteTree(bsssegment);
  702. Writetree(importssection);
  703. { exports are written by DLLTOOL
  704. if we use it so don't insert it twice (PM) }
  705. if not UseDeffileForExport and assigned(exportssection) then
  706. Writetree(exportssection);
  707. Writetree(resourcesection);
  708. countlabelref:=true;
  709. AsmLn;
  710. {$ifdef EXTDEBUG}
  711. if assigned(current_module.mainsource) then
  712. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  713. {$endif EXTDEBUG}
  714. end;
  715. {*****************************************************************************
  716. Initialize
  717. *****************************************************************************}
  718. const
  719. as_i386_nasmcoff_info : tasminfo =
  720. (
  721. id : as_i386_nasmcoff;
  722. idtxt : 'NASMCOFF';
  723. asmbin : 'nasm';
  724. asmcmd : '-f coff -o $OBJ $ASM';
  725. supported_target : target_i386_go32v2;
  726. outputbinary: false;
  727. allowdirect : true;
  728. externals : true;
  729. needar : true;
  730. labelprefix_only_inside_procedure: false;
  731. labelprefix : '..@';
  732. comment : '; ';
  733. secnames : ('',
  734. '.text','.data','.bss',
  735. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  736. '.stab','.stabstr')
  737. );
  738. as_i386_nasmwin32_info : tasminfo =
  739. (
  740. id : as_i386_nasmwin32;
  741. idtxt : 'NASMWIN32';
  742. asmbin : 'nasm';
  743. asmcmd : '-f win32 -o $OBJ $ASM';
  744. supported_target : target_i386_win32;
  745. outputbinary: false;
  746. allowdirect : true;
  747. externals : true;
  748. needar : true;
  749. labelprefix_only_inside_procedure: false;
  750. labelprefix : '..@';
  751. comment : '; ';
  752. secnames : ('',
  753. '.text','.data','.bss',
  754. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  755. '.stab','.stabstr')
  756. );
  757. as_i386_nasmobj_info : tasminfo =
  758. (
  759. id : as_i386_nasmobj;
  760. idtxt : 'NASMOBJ';
  761. asmbin : 'nasm';
  762. asmcmd : '-f obj -o $OBJ $ASM';
  763. supported_target : target_any; { what should I write here ?? }
  764. outputbinary: false;
  765. allowdirect : true;
  766. externals : true;
  767. needar : true;
  768. labelprefix_only_inside_procedure: false;
  769. labelprefix : '..@';
  770. comment : '; ';
  771. secnames : ('',
  772. '.text','.data','.bss',
  773. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  774. '.stab','.stabstr')
  775. );
  776. as_i386_nasmwdosx_info : tasminfo =
  777. (
  778. id : as_i386_nasmwdosx;
  779. idtxt : 'NASMWDOSX';
  780. asmbin : 'nasm';
  781. asmcmd : '-f win32 -o $OBJ $ASM';
  782. supported_target : target_i386_wdosx;
  783. outputbinary: false;
  784. allowdirect : true;
  785. externals : true;
  786. needar : true;
  787. labelprefix_only_inside_procedure: false;
  788. labelprefix : '..@';
  789. comment : '; ';
  790. secnames : ('',
  791. '.text','.data','.bss',
  792. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  793. '.stab','.stabstr')
  794. );
  795. as_i386_nasmelf_info : tasminfo =
  796. (
  797. id : as_i386_nasmelf;
  798. idtxt : 'NASMELF';
  799. asmbin : 'nasm';
  800. asmcmd : '-f elf -o $OBJ $ASM';
  801. supported_target : target_i386_linux;
  802. outputbinary: false;
  803. allowdirect : true;
  804. externals : true;
  805. needar : true;
  806. labelprefix_only_inside_procedure: false;
  807. labelprefix : '..@';
  808. comment : '; ';
  809. secnames : ('',
  810. '.text','.data','.bss',
  811. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  812. '.stab','.stabstr')
  813. );
  814. initialization
  815. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  816. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  817. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  818. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  819. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  820. end.
  821. {
  822. $Log$
  823. Revision 1.19 2002-05-16 19:46:50 carl
  824. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  825. + try to fix temp allocation (still in ifdef)
  826. + generic constructor calls
  827. + start of tassembler / tmodulebase class cleanup
  828. Revision 1.17 2002/05/12 16:53:16 peter
  829. * moved entry and exitcode to ncgutil and cgobj
  830. * foreach gets extra argument for passing local data to the
  831. iterator function
  832. * -CR checks also class typecasts at runtime by changing them
  833. into as
  834. * fixed compiler to cycle with the -CR option
  835. * fixed stabs with elf writer, finally the global variables can
  836. be watched
  837. * removed a lot of routines from cga unit and replaced them by
  838. calls to cgobj
  839. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  840. u32bit then the other is typecasted also to u32bit without giving
  841. a rangecheck warning/error.
  842. * fixed pascal calling method with reversing also the high tree in
  843. the parast, detected by tcalcst3 test
  844. Revision 1.16 2002/04/15 19:12:09 carl
  845. + target_info.size_of_pointer -> pointer_size
  846. + some cleanup of unused types/variables
  847. * move several constants from cpubase to their specific units
  848. (where they are used)
  849. + att_Reg2str -> gas_reg2str
  850. + int_reg2str -> std_reg2str
  851. Revision 1.15 2002/04/14 16:58:41 carl
  852. + att_reg2str -> gas_reg2str
  853. Revision 1.14 2002/04/04 18:27:37 carl
  854. + added wdosx support (patch from Pavel)
  855. Revision 1.13 2002/04/02 17:11:33 peter
  856. * tlocation,treference update
  857. * LOC_CONSTANT added for better constant handling
  858. * secondadd splitted in multiple routines
  859. * location_force_reg added for loading a location to a register
  860. of a specified size
  861. * secondassignment parses now first the right and then the left node
  862. (this is compatible with Kylix). This saves a lot of push/pop especially
  863. with string operations
  864. * adapted some routines to use the new cg methods
  865. Revision 1.12 2001/12/29 15:29:58 jonas
  866. * powerpc/cgcpu.pas compiles :)
  867. * several powerpc-related fixes
  868. * cpuasm unit is now based on common tainst unit
  869. + nppcmat unit for powerpc (almost complete)
  870. Revision 1.11 2001/05/06 17:13:23 jonas
  871. * completed incomplete typed constant records
  872. Revision 1.10 2001/04/21 15:33:03 peter
  873. * stupid bug, finalization to initialization renaming
  874. Revision 1.9 2001/04/21 12:09:00 peter
  875. * fixed bug 1472 (merged)
  876. Revision 1.8 2001/04/18 22:02:00 peter
  877. * registration of targets and assemblers
  878. Revision 1.7 2001/04/13 01:22:17 peter
  879. * symtable change to classes
  880. * range check generation and errors fixed, make cycle DEBUG=1 works
  881. * memory leaks fixed
  882. Revision 1.6 2001/03/05 21:39:11 peter
  883. * changed to class with common TAssembler also for internal assembler
  884. Revision 1.5 2001/02/20 21:36:39 peter
  885. * tasm/masm fixes merged
  886. Revision 1.4 2001/01/13 20:24:24 peter
  887. * fixed operand order that got mixed up for external writers after
  888. my previous assembler block valid instruction check
  889. Revision 1.3 2000/12/25 00:07:31 peter
  890. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  891. tlinkedlist objects)
  892. Revision 1.2 2000/11/29 00:30:43 florian
  893. * unused units removed from uses clause
  894. * some changes for widestrings
  895. Revision 1.1 2000/10/15 09:47:42 peter
  896. * moved to i386/
  897. Revision 1.6 2000/09/24 15:06:11 peter
  898. * use defines.inc
  899. Revision 1.5 2000/08/27 16:11:49 peter
  900. * moved some util functions from globals,cobjects to cutils
  901. * splitted files into finput,fmodule
  902. Revision 1.4 2000/08/20 17:38:21 peter
  903. * smartlinking fixed for linux (merged)
  904. Revision 1.3 2000/07/13 12:08:24 michael
  905. + patched to 1.1.0 with former 1.09patch from peter
  906. Revision 1.2 2000/07/13 11:32:30 michael
  907. + removed logs
  908. }