ag386nsm.pas 29 KB

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