ag386nsm.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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.20 2002-05-18 13:34:21 peter
  824. * readded missing revisions
  825. Revision 1.19 2002/05/16 19:46:50 carl
  826. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  827. + try to fix temp allocation (still in ifdef)
  828. + generic constructor calls
  829. + start of tassembler / tmodulebase class cleanup
  830. Revision 1.17 2002/05/12 16:53:16 peter
  831. * moved entry and exitcode to ncgutil and cgobj
  832. * foreach gets extra argument for passing local data to the
  833. iterator function
  834. * -CR checks also class typecasts at runtime by changing them
  835. into as
  836. * fixed compiler to cycle with the -CR option
  837. * fixed stabs with elf writer, finally the global variables can
  838. be watched
  839. * removed a lot of routines from cga unit and replaced them by
  840. calls to cgobj
  841. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  842. u32bit then the other is typecasted also to u32bit without giving
  843. a rangecheck warning/error.
  844. * fixed pascal calling method with reversing also the high tree in
  845. the parast, detected by tcalcst3 test
  846. Revision 1.16 2002/04/15 19:12:09 carl
  847. + target_info.size_of_pointer -> pointer_size
  848. + some cleanup of unused types/variables
  849. * move several constants from cpubase to their specific units
  850. (where they are used)
  851. + att_Reg2str -> gas_reg2str
  852. + int_reg2str -> std_reg2str
  853. Revision 1.15 2002/04/14 16:58:41 carl
  854. + att_reg2str -> gas_reg2str
  855. Revision 1.14 2002/04/04 18:27:37 carl
  856. + added wdosx support (patch from Pavel)
  857. Revision 1.13 2002/04/02 17:11:33 peter
  858. * tlocation,treference update
  859. * LOC_CONSTANT added for better constant handling
  860. * secondadd splitted in multiple routines
  861. * location_force_reg added for loading a location to a register
  862. of a specified size
  863. * secondassignment parses now first the right and then the left node
  864. (this is compatible with Kylix). This saves a lot of push/pop especially
  865. with string operations
  866. * adapted some routines to use the new cg methods
  867. }