ag386nsm.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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
  23. cpubase,
  24. aasmbase,aasmtai,aasmcpu,assemble;
  25. type
  26. T386NasmAssembler = class(texternalassembler)
  27. private
  28. procedure WriteReference(var ref : treference);
  29. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  30. procedure WriteOper_jmp(const o:toper; op : tasmop);
  31. procedure WriteSection(atype:tasmsectiontype;const aname:string);
  32. public
  33. procedure WriteTree(p:taasmoutput);override;
  34. procedure WriteAsmList;override;
  35. procedure WriteExternals;
  36. end;
  37. implementation
  38. uses
  39. {$ifdef delphi}
  40. sysutils,
  41. {$endif}
  42. cutils,globtype,globals,systems,cclasses,
  43. fmodule,finput,verbose,cpuinfo,cgbase
  44. ;
  45. const
  46. line_length = 64;
  47. nasm_regname_table : array[tregisterindex] of string[7] = (
  48. {r386nasm.inc contains the Nasm name of each register.}
  49. {$i r386nasm.inc}
  50. );
  51. var
  52. lastfileinfo : tfileposinfo;
  53. infile,
  54. lastinfile : tinputfile;
  55. function nasm_regname(r:Tregister):string;
  56. var
  57. p : tregisterindex;
  58. begin
  59. p:=findreg_by_number(r);
  60. if p<>0 then
  61. result:=nasm_regname_table[p]
  62. else
  63. result:=generic_regname(r);
  64. end;
  65. function fixline(s:string):string;
  66. {
  67. return s with all leading and ending spaces and tabs removed
  68. }
  69. var
  70. i,j,k : longint;
  71. begin
  72. i:=length(s);
  73. while (i>0) and (s[i] in [#9,' ']) do
  74. dec(i);
  75. j:=1;
  76. while (j<i) and (s[j] in [#9,' ']) do
  77. inc(j);
  78. for k:=j to i do
  79. if s[k] in [#0..#31,#127..#255] then
  80. s[k]:='.';
  81. fixline:=Copy(s,j,i-j+1);
  82. end;
  83. function single2str(d : single) : string;
  84. var
  85. hs : string;
  86. p : byte;
  87. begin
  88. str(d,hs);
  89. { nasm expects a lowercase e }
  90. p:=pos('E',hs);
  91. if p>0 then
  92. hs[p]:='e';
  93. p:=pos('+',hs);
  94. if p>0 then
  95. delete(hs,p,1);
  96. single2str:=lower(hs);
  97. end;
  98. function double2str(d : double) : string;
  99. var
  100. hs : string;
  101. p : byte;
  102. begin
  103. str(d,hs);
  104. { nasm expects a lowercase e }
  105. p:=pos('E',hs);
  106. if p>0 then
  107. hs[p]:='e';
  108. p:=pos('+',hs);
  109. if p>0 then
  110. delete(hs,p,1);
  111. double2str:=lower(hs);
  112. end;
  113. function extended2str(e : extended) : string;
  114. var
  115. hs : string;
  116. p : byte;
  117. begin
  118. str(e,hs);
  119. { nasm expects a lowercase e }
  120. p:=pos('E',hs);
  121. if p>0 then
  122. hs[p]:='e';
  123. p:=pos('+',hs);
  124. if p>0 then
  125. delete(hs,p,1);
  126. extended2str:=lower(hs);
  127. end;
  128. function comp2str(d : bestreal) : string;
  129. type
  130. pdouble = ^double;
  131. var
  132. c : comp;
  133. dd : pdouble;
  134. begin
  135. {$ifdef FPC}
  136. c:=comp(d);
  137. {$else}
  138. c:=d;
  139. {$endif}
  140. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  141. comp2str:=double2str(dd^);
  142. end;
  143. function sizestr(s:topsize;dest:boolean):string;
  144. begin
  145. case s of
  146. S_B : sizestr:='byte ';
  147. S_W : sizestr:='word ';
  148. S_L : sizestr:='dword ';
  149. S_IS : sizestr:='word ';
  150. S_IL : sizestr:='dword ';
  151. S_IQ : sizestr:='qword ';
  152. S_FS : sizestr:='dword ';
  153. S_FL : sizestr:='qword ';
  154. S_FX : sizestr:='tword ';
  155. S_BW : if dest then
  156. sizestr:='word '
  157. else
  158. sizestr:='byte ';
  159. S_BL : if dest then
  160. sizestr:='dword '
  161. else
  162. sizestr:='byte ';
  163. S_WL : if dest then
  164. sizestr:='dword '
  165. else
  166. sizestr:='word ';
  167. else { S_NO }
  168. sizestr:='';
  169. end;
  170. end;
  171. Function PadTabs(const p:string;addch:char):string;
  172. var
  173. s : string;
  174. i : longint;
  175. begin
  176. i:=length(p);
  177. if addch<>#0 then
  178. begin
  179. inc(i);
  180. s:=p+addch;
  181. end
  182. else
  183. s:=p;
  184. if i<8 then
  185. PadTabs:=s+#9#9
  186. else
  187. PadTabs:=s+#9;
  188. end;
  189. {****************************************************************************
  190. T386NasmAssembler
  191. ****************************************************************************}
  192. procedure T386NasmAssembler.WriteReference(var ref : treference);
  193. var
  194. first : boolean;
  195. begin
  196. with ref do
  197. begin
  198. AsmWrite('[');
  199. first:=true;
  200. if (segment<>NR_NO) then
  201. AsmWrite(nasm_regname(segment)+':');
  202. if assigned(symbol) then
  203. begin
  204. AsmWrite(symbol.name);
  205. first:=false;
  206. end;
  207. if (base<>NR_NO) then
  208. begin
  209. if not(first) then
  210. AsmWrite('+')
  211. else
  212. first:=false;
  213. AsmWrite(nasm_regname(base))
  214. end;
  215. if (index<>NR_NO) then
  216. begin
  217. if not(first) then
  218. AsmWrite('+')
  219. else
  220. first:=false;
  221. AsmWrite(nasm_regname(index));
  222. if scalefactor<>0 then
  223. AsmWrite('*'+tostr(scalefactor));
  224. end;
  225. if offset<0 then
  226. begin
  227. AsmWrite(tostr(offset));
  228. first:=false;
  229. end
  230. else if (offset>0) then
  231. begin
  232. AsmWrite('+'+tostr(offset));
  233. first:=false;
  234. end;
  235. if first then
  236. AsmWrite('0');
  237. AsmWrite(']');
  238. end;
  239. end;
  240. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  241. begin
  242. case o.typ of
  243. top_reg :
  244. AsmWrite(nasm_regname(o.reg));
  245. top_const :
  246. begin
  247. if (ops=1) and (opcode<>A_RET) then
  248. AsmWrite(sizestr(s,dest));
  249. AsmWrite(tostr(longint(o.val)));
  250. end;
  251. top_ref :
  252. begin
  253. if o.ref^.refaddr=addr_no then
  254. begin
  255. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  256. (opcode = A_LSS) or (opcode = A_LFS) or
  257. (opcode = A_LES) or (opcode = A_LDS) or
  258. (opcode = A_SHR) or (opcode = A_SHL) or
  259. (opcode = A_SAR) or (opcode = A_SAL) or
  260. (opcode = A_OUT) or (opcode = A_IN)) then
  261. AsmWrite(sizestr(s,dest));
  262. WriteReference(o.ref^);
  263. end
  264. else
  265. begin
  266. asmwrite('dword ');
  267. if assigned(o.ref^.symbol) then
  268. begin
  269. asmwrite(o.ref^.symbol.name);
  270. if o.ref^.offset=0 then
  271. exit;
  272. end;
  273. if o.ref^.offset>0 then
  274. asmwrite('+');
  275. asmwrite(tostr(o.ref^.offset));
  276. end;
  277. end;
  278. else
  279. internalerror(10001);
  280. end;
  281. end;
  282. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  283. begin
  284. case o.typ of
  285. top_reg :
  286. AsmWrite(nasm_regname(o.reg));
  287. top_ref :
  288. if o.ref^.refaddr=addr_no then
  289. WriteReference(o.ref^)
  290. else
  291. begin
  292. if not(
  293. (op=A_JCXZ) or (op=A_JECXZ) or
  294. (op=A_LOOP) or (op=A_LOOPE) or
  295. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  296. (op=A_LOOPZ)
  297. ) then
  298. AsmWrite('NEAR ');
  299. AsmWrite(o.ref^.symbol.name);
  300. if o.ref^.offset>0 then
  301. AsmWrite('+'+tostr(o.ref^.offset))
  302. else
  303. if o.ref^.offset<0 then
  304. AsmWrite(tostr(o.ref^.offset));
  305. end;
  306. top_const :
  307. AsmWrite(tostr(aint(o.val)));
  308. else
  309. internalerror(10001);
  310. end;
  311. end;
  312. var
  313. LastSecType : TAsmSectionType;
  314. const
  315. ait_const2str : array[ait_const_128bit..ait_const_indirect_symbol] of string[20]=(
  316. #9'FIXME128',#9'FIXME64',#9'DD'#9,#9'DW'#9,#9'DB'#9,
  317. #9'FIXMESLEB',#9'FIXEMEULEB',
  318. #9'RVA'#9,#9'FIXMEINDIRECT'#9
  319. );
  320. procedure T386NasmAssembler.WriteSection(atype:tasmsectiontype;const aname:string);
  321. const
  322. secnames : array[tasmsectiontype] of string[12] = ('',
  323. '.text','.data','.rodata','.bss',
  324. 'common',
  325. '.note',
  326. '.stab','.stabstr',
  327. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  328. '.eh_frame',
  329. '.debug_frame'
  330. );
  331. begin
  332. AsmLn;
  333. AsmWrite('SECTION ');
  334. AsmWrite(secnames[atype]);
  335. if (atype<>sec_bss) and (aname<>'') then
  336. begin
  337. AsmWrite('.');
  338. AsmWrite(aname);
  339. end;
  340. AsmLn;
  341. LasTSecType:=atype;
  342. end;
  343. procedure T386NasmAssembler.WriteTree(p:taasmoutput);
  344. const
  345. regallocstr : array[tregalloctype] of string[10]=(' released',' allocated',' sync',' resized');
  346. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  347. var
  348. s : string;
  349. hp : tai;
  350. hp1 : tailineinfo;
  351. counter,
  352. lines,
  353. i,j,l : longint;
  354. InlineLevel : longint;
  355. consttyp : taitype;
  356. found,
  357. do_line,
  358. quoted : boolean;
  359. begin
  360. if not assigned(p) then
  361. exit;
  362. InlineLevel:=0;
  363. { lineinfo is only needed for codesegment (PFV) }
  364. do_line:=(cs_asm_source in aktglobalswitches) or
  365. ((cs_lineinfo in aktmoduleswitches)
  366. and (p=codesegment));
  367. hp:=tai(p.first);
  368. while assigned(hp) do
  369. begin
  370. if not(hp.typ in SkipLineInfo) then
  371. begin
  372. hp1:=hp as tailineinfo;
  373. aktfilepos:=hp1.fileinfo;
  374. if do_line then
  375. begin
  376. { load infile }
  377. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  378. begin
  379. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  380. if assigned(infile) then
  381. begin
  382. { open only if needed !! }
  383. if (cs_asm_source in aktglobalswitches) then
  384. infile.open;
  385. end;
  386. { avoid unnecessary reopens of the same file !! }
  387. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  388. { be sure to change line !! }
  389. lastfileinfo.line:=-1;
  390. end;
  391. { write source }
  392. if (cs_asm_source in aktglobalswitches) and
  393. assigned(infile) then
  394. begin
  395. if (infile<>lastinfile) then
  396. begin
  397. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  398. if assigned(lastinfile) then
  399. lastinfile.close;
  400. end;
  401. if (hp1.fileinfo.line<>lastfileinfo.line) and
  402. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  403. begin
  404. if (hp1.fileinfo.line<>0) and
  405. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  406. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  407. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  408. { set it to a negative value !
  409. to make that is has been read already !! PM }
  410. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  411. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  412. end;
  413. end;
  414. lastfileinfo:=hp1.fileinfo;
  415. lastinfile:=infile;
  416. end;
  417. end;
  418. case hp.typ of
  419. ait_comment :
  420. Begin
  421. AsmWrite(target_asm.comment);
  422. AsmWritePChar(tai_comment(hp).str);
  423. AsmLn;
  424. End;
  425. ait_regalloc :
  426. begin
  427. if (cs_asm_regalloc in aktglobalswitches) then
  428. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  429. regallocstr[tai_regalloc(hp).ratype]);
  430. end;
  431. ait_tempalloc :
  432. begin
  433. if (cs_asm_tempalloc in aktglobalswitches) then
  434. begin
  435. {$ifdef EXTDEBUG}
  436. if assigned(tai_tempalloc(hp).problem) then
  437. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  438. tostr(tai_tempalloc(hp).tempsize)+')')
  439. else
  440. {$endif EXTDEBUG}
  441. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  442. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  443. end;
  444. end;
  445. ait_section :
  446. begin
  447. if tai_section(hp).sectype<>sec_none then
  448. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  449. LasTSecType:=tai_section(hp).sectype;
  450. end;
  451. ait_align :
  452. begin
  453. { nasm gives warnings when it finds align in bss as it
  454. wants to store data }
  455. if (lastsectype<>sec_bss) and
  456. (tai_align(hp).aligntype>1) then
  457. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  458. end;
  459. ait_datablock :
  460. begin
  461. if tai_datablock(hp).is_global then
  462. begin
  463. AsmWrite(#9'GLOBAL ');
  464. AsmWriteLn(tai_datablock(hp).sym.name);
  465. end;
  466. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  467. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  468. end;
  469. ait_const_uleb128bit,
  470. ait_const_sleb128bit,
  471. ait_const_128bit,
  472. ait_const_64bit,
  473. ait_const_32bit,
  474. ait_const_16bit,
  475. ait_const_8bit,
  476. ait_const_rva_symbol,
  477. ait_const_indirect_symbol :
  478. begin
  479. AsmWrite(ait_const2str[hp.typ]);
  480. consttyp:=hp.typ;
  481. l:=0;
  482. repeat
  483. if assigned(tai_const(hp).sym) then
  484. begin
  485. if assigned(tai_const(hp).endsym) then
  486. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  487. else
  488. s:=tai_const(hp).sym.name;
  489. if tai_const(hp).value<>0 then
  490. s:=s+tostr_with_plus(tai_const(hp).value);
  491. end
  492. else
  493. s:=tostr(tai_const(hp).value);
  494. AsmWrite(s);
  495. inc(l,length(s));
  496. if (l>line_length) or
  497. (hp.next=nil) or
  498. (tai(hp.next).typ<>consttyp) then
  499. break;
  500. hp:=tai(hp.next);
  501. AsmWrite(',');
  502. until false;
  503. AsmLn;
  504. end;
  505. ait_real_32bit :
  506. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  507. ait_real_64bit :
  508. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  509. ait_real_80bit :
  510. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  511. ait_comp_64bit :
  512. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  513. ait_string :
  514. begin
  515. counter := 0;
  516. lines := tai_string(hp).len div line_length;
  517. { separate lines in different parts }
  518. if tai_string(hp).len > 0 then
  519. Begin
  520. for j := 0 to lines-1 do
  521. begin
  522. AsmWrite(#9#9'DB'#9);
  523. quoted:=false;
  524. for i:=counter to counter+line_length-1 do
  525. begin
  526. { it is an ascii character. }
  527. if (ord(tai_string(hp).str[i])>31) and
  528. (ord(tai_string(hp).str[i])<128) and
  529. (tai_string(hp).str[i]<>'"') then
  530. begin
  531. if not(quoted) then
  532. begin
  533. if i>counter then
  534. AsmWrite(',');
  535. AsmWrite('"');
  536. end;
  537. AsmWrite(tai_string(hp).str[i]);
  538. quoted:=true;
  539. end { if > 31 and < 128 and ord('"') }
  540. else
  541. begin
  542. if quoted then
  543. AsmWrite('"');
  544. if i>counter then
  545. AsmWrite(',');
  546. quoted:=false;
  547. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  548. end;
  549. end; { end for i:=0 to... }
  550. if quoted then AsmWrite('"');
  551. AsmWrite(target_info.newline);
  552. inc(counter,line_length);
  553. end; { end for j:=0 ... }
  554. { do last line of lines }
  555. if counter<tai_string(hp).len then
  556. AsmWrite(#9#9'DB'#9);
  557. quoted:=false;
  558. for i:=counter to tai_string(hp).len-1 do
  559. begin
  560. { it is an ascii character. }
  561. if (ord(tai_string(hp).str[i])>31) and
  562. (ord(tai_string(hp).str[i])<128) and
  563. (tai_string(hp).str[i]<>'"') then
  564. begin
  565. if not(quoted) then
  566. begin
  567. if i>counter then
  568. AsmWrite(',');
  569. AsmWrite('"');
  570. end;
  571. AsmWrite(tai_string(hp).str[i]);
  572. quoted:=true;
  573. end { if > 31 and < 128 and " }
  574. else
  575. begin
  576. if quoted then
  577. AsmWrite('"');
  578. if i>counter then
  579. AsmWrite(',');
  580. quoted:=false;
  581. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  582. end;
  583. end; { end for i:=0 to... }
  584. if quoted then
  585. AsmWrite('"');
  586. end;
  587. AsmLn;
  588. end;
  589. ait_label :
  590. begin
  591. if tai_label(hp).l.is_used then
  592. AsmWriteLn(tai_label(hp).l.name+':');
  593. end;
  594. ait_direct :
  595. begin
  596. AsmWritePChar(tai_direct(hp).str);
  597. AsmLn;
  598. end;
  599. ait_symbol :
  600. begin
  601. if tai_symbol(hp).is_global then
  602. begin
  603. AsmWrite(#9'GLOBAL ');
  604. AsmWriteLn(tai_symbol(hp).sym.name);
  605. end;
  606. AsmWrite(tai_symbol(hp).sym.name);
  607. if assigned(hp.next) and not(tai(hp.next).typ in
  608. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  609. ait_const_rva_symbol,
  610. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  611. AsmWriteLn(':')
  612. end;
  613. ait_symbol_end : ;
  614. ait_instruction :
  615. begin
  616. taicpu(hp).CheckNonCommutativeOpcodes;
  617. { We need intel order, no At&t }
  618. taicpu(hp).SetOperandOrder(op_intel);
  619. s:='';
  620. if ((taicpu(hp).opcode=A_FADDP) or
  621. (taicpu(hp).opcode=A_FMULP))
  622. and (taicpu(hp).ops=0) then
  623. begin
  624. taicpu(hp).allocate_oper(2);
  625. taicpu(hp).oper[0]^.typ:=top_reg;
  626. taicpu(hp).oper[0]^.reg:=NR_ST1;
  627. taicpu(hp).oper[1]^.typ:=top_reg;
  628. taicpu(hp).oper[1]^.reg:=NR_ST;
  629. end;
  630. if taicpu(hp).opcode=A_FWAIT then
  631. AsmWriteln(#9#9'DB'#9'09bh')
  632. else
  633. begin
  634. { We need to explicitely set
  635. word prefix to get selectors
  636. to be pushed in 2 bytes PM }
  637. if (taicpu(hp).opsize=S_W) and
  638. ((taicpu(hp).opcode=A_PUSH) or
  639. (taicpu(hp).opcode=A_POP)) and
  640. (taicpu(hp).oper[0]^.typ=top_reg) and
  641. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  642. AsmWriteln(#9#9'DB'#9'066h');
  643. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  644. if taicpu(hp).ops<>0 then
  645. begin
  646. if is_calljmp(taicpu(hp).opcode) then
  647. begin
  648. AsmWrite(#9);
  649. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  650. end
  651. else
  652. begin
  653. for i:=0 to taicpu(hp).ops-1 do
  654. begin
  655. if i=0 then
  656. AsmWrite(#9)
  657. else
  658. AsmWrite(',');
  659. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  660. end;
  661. end;
  662. end;
  663. AsmLn;
  664. end;
  665. end;
  666. {$ifdef GDB}
  667. ait_stabn,
  668. ait_stabs,
  669. ait_force_line,
  670. ait_stab_function_name : ;
  671. {$endif GDB}
  672. ait_cutobject :
  673. begin
  674. { only reset buffer if nothing has changed }
  675. if AsmSize=AsmStartSize then
  676. AsmClear
  677. else
  678. begin
  679. AsmClose;
  680. DoAssemble;
  681. AsmCreate(tai_cutobject(hp).place);
  682. end;
  683. { avoid empty files }
  684. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  685. begin
  686. if tai(hp.next).typ=ait_section then
  687. lasTSectype:=tai_section(hp.next).sectype;
  688. hp:=tai(hp.next);
  689. end;
  690. if lasTSectype<>sec_none then
  691. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  692. AsmStartSize:=AsmSize;
  693. end;
  694. ait_marker :
  695. if tai_marker(hp).kind=InlineStart then
  696. inc(InlineLevel)
  697. else if tai_marker(hp).kind=InlineEnd then
  698. dec(InlineLevel);
  699. else
  700. internalerror(10000);
  701. end;
  702. hp:=tai(hp.next);
  703. end;
  704. end;
  705. var
  706. currentasmlist : TExternalAssembler;
  707. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  708. begin
  709. if tasmsymbol(p).defbind=AB_EXTERNAL then
  710. currentasmlist.AsmWriteln('EXTERN'#9+p.name);
  711. end;
  712. procedure T386NasmAssembler.WriteExternals;
  713. begin
  714. currentasmlist:=self;
  715. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  716. end;
  717. procedure T386NasmAssembler.WriteAsmList;
  718. begin
  719. {$ifdef EXTDEBUG}
  720. if assigned(current_module.mainsource) then
  721. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  722. {$endif}
  723. LasTSecType:=sec_none;
  724. AsmWriteLn('BITS 32');
  725. AsmLn;
  726. lastfileinfo.line:=-1;
  727. lastfileinfo.fileindex:=0;
  728. lastinfile:=nil;
  729. WriteExternals;
  730. { Nasm doesn't support stabs
  731. WriteTree(debuglist);}
  732. WriteTree(codesegment);
  733. WriteTree(datasegment);
  734. WriteTree(consts);
  735. WriteTree(rttilist);
  736. WriteTree(resourcestringlist);
  737. WriteTree(bsssegment);
  738. Writetree(importssection);
  739. { exports are written by DLLTOOL
  740. if we use it so don't insert it twice (PM) }
  741. if not UseDeffileForExports and assigned(exportssection) then
  742. Writetree(exportssection);
  743. Writetree(resourcesection);
  744. AsmLn;
  745. {$ifdef EXTDEBUG}
  746. if assigned(current_module.mainsource) then
  747. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  748. {$endif EXTDEBUG}
  749. end;
  750. {*****************************************************************************
  751. Initialize
  752. *****************************************************************************}
  753. const
  754. as_i386_nasmcoff_info : tasminfo =
  755. (
  756. id : as_i386_nasmcoff;
  757. idtxt : 'NASMCOFF';
  758. asmbin : 'nasm';
  759. asmcmd : '-f coff -o $OBJ $ASM';
  760. supported_target : system_i386_go32v2;
  761. flags : [af_allowdirect,af_needar];
  762. labelprefix : '..@';
  763. comment : '; ';
  764. );
  765. as_i386_nasmwin32_info : tasminfo =
  766. (
  767. id : as_i386_nasmwin32;
  768. idtxt : 'NASMWIN32';
  769. asmbin : 'nasm';
  770. asmcmd : '-f win32 -o $OBJ $ASM';
  771. supported_target : system_i386_win32;
  772. flags : [af_allowdirect,af_needar];
  773. labelprefix : '..@';
  774. comment : '; ';
  775. );
  776. as_i386_nasmobj_info : tasminfo =
  777. (
  778. id : as_i386_nasmobj;
  779. idtxt : 'NASMOBJ';
  780. asmbin : 'nasm';
  781. asmcmd : '-f obj -o $OBJ $ASM';
  782. supported_target : system_any; { what should I write here ?? }
  783. flags : [af_allowdirect,af_needar];
  784. labelprefix : '..@';
  785. comment : '; ';
  786. );
  787. as_i386_nasmwdosx_info : tasminfo =
  788. (
  789. id : as_i386_nasmwdosx;
  790. idtxt : 'NASMWDOSX';
  791. asmbin : 'nasm';
  792. asmcmd : '-f win32 -o $OBJ $ASM';
  793. supported_target : system_i386_wdosx;
  794. flags : [af_allowdirect,af_needar];
  795. labelprefix : '..@';
  796. comment : '; ';
  797. );
  798. as_i386_nasmelf_info : tasminfo =
  799. (
  800. id : as_i386_nasmelf;
  801. idtxt : 'NASMELF';
  802. asmbin : 'nasm';
  803. asmcmd : '-f elf -o $OBJ $ASM';
  804. supported_target : system_i386_linux;
  805. flags : [af_allowdirect,af_needar];
  806. labelprefix : '..@';
  807. comment : '; ';
  808. );
  809. as_i386_nasmbeos_info : tasminfo =
  810. (
  811. id : as_i386_nasmbeos;
  812. idtxt : 'NASMELF';
  813. asmbin : 'nasm';
  814. asmcmd : '-f elf -o $OBJ $ASM';
  815. supported_target : system_i386_beos;
  816. flags : [af_allowdirect,af_needar];
  817. labelprefix : '..@';
  818. comment : '; ';
  819. );
  820. initialization
  821. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  822. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  823. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  824. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  825. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  826. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  827. end.
  828. {
  829. $Log$
  830. Revision 1.48 2004-09-26 17:45:30 peter
  831. * simple regvar support, not yet finished
  832. Revision 1.47 2004/06/20 08:55:31 florian
  833. * logs truncated
  834. Revision 1.46 2004/06/16 20:07:10 florian
  835. * dwarf branch merged
  836. Revision 1.45 2004/05/22 23:34:28 peter
  837. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  838. Revision 1.44.2.7 2004/05/03 14:59:58 peter
  839. * no dlltool needed for win32 linking executables
  840. Revision 1.44.2.6 2004/05/01 16:02:10 peter
  841. * POINTER_SIZE replaced with sizeof(aint)
  842. * aint,aword,tconst*int moved to globtype
  843. Revision 1.44.2.5 2004/04/29 19:07:22 peter
  844. * compile fixes
  845. }