ag386nsm.pas 30 KB

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