ag386nsm.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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,aasmdata,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:TAsmList);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 : longint;
  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 : longint;
  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 : longint;
  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. c:=comp(d);
  132. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  133. comp2str:=double2str(dd^);
  134. end;
  135. function sizestr(s:topsize;dest:boolean):string;
  136. begin
  137. case s of
  138. S_B : sizestr:='byte ';
  139. S_W : sizestr:='word ';
  140. S_L : sizestr:='dword ';
  141. S_IS : sizestr:='word ';
  142. S_IL : sizestr:='dword ';
  143. S_IQ : sizestr:='qword ';
  144. S_FS : sizestr:='dword ';
  145. S_FL : sizestr:='qword ';
  146. S_FX : sizestr:='tword ';
  147. S_BW : if dest then
  148. sizestr:='word '
  149. else
  150. sizestr:='byte ';
  151. S_BL : if dest then
  152. sizestr:='dword '
  153. else
  154. sizestr:='byte ';
  155. S_WL : if dest then
  156. sizestr:='dword '
  157. else
  158. sizestr:='word ';
  159. else { S_NO }
  160. sizestr:='';
  161. end;
  162. end;
  163. Function PadTabs(const p:string;addch:char):string;
  164. var
  165. s : string;
  166. i : longint;
  167. begin
  168. i:=length(p);
  169. if addch<>#0 then
  170. begin
  171. inc(i);
  172. s:=p+addch;
  173. end
  174. else
  175. s:=p;
  176. if i<8 then
  177. PadTabs:=s+#9#9
  178. else
  179. PadTabs:=s+#9;
  180. end;
  181. {****************************************************************************
  182. T386NasmAssembler
  183. ****************************************************************************}
  184. procedure T386NasmAssembler.WriteReference(var ref : treference);
  185. var
  186. first : boolean;
  187. begin
  188. with ref do
  189. begin
  190. AsmWrite('[');
  191. first:=true;
  192. if (segment<>NR_NO) then
  193. AsmWrite(nasm_regname(segment)+':');
  194. if assigned(symbol) then
  195. begin
  196. AsmWrite(symbol.name);
  197. first:=false;
  198. end;
  199. if (base<>NR_NO) then
  200. begin
  201. if not(first) then
  202. AsmWrite('+')
  203. else
  204. first:=false;
  205. AsmWrite(nasm_regname(base))
  206. end;
  207. if (index<>NR_NO) then
  208. begin
  209. if not(first) then
  210. AsmWrite('+')
  211. else
  212. first:=false;
  213. AsmWrite(nasm_regname(index));
  214. if scalefactor<>0 then
  215. AsmWrite('*'+tostr(scalefactor));
  216. end;
  217. if offset<0 then
  218. begin
  219. AsmWrite(tostr(offset));
  220. first:=false;
  221. end
  222. else if (offset>0) then
  223. begin
  224. AsmWrite('+'+tostr(offset));
  225. first:=false;
  226. end;
  227. if first then
  228. AsmWrite('0');
  229. AsmWrite(']');
  230. end;
  231. end;
  232. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  233. begin
  234. case o.typ of
  235. top_reg :
  236. AsmWrite(nasm_regname(o.reg));
  237. top_const :
  238. begin
  239. if (ops=1) and (opcode<>A_RET) then
  240. AsmWrite(sizestr(s,dest));
  241. AsmWrite(tostr(longint(o.val)));
  242. end;
  243. top_ref :
  244. begin
  245. if o.ref^.refaddr=addr_no then
  246. begin
  247. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  248. (opcode = A_LSS) or (opcode = A_LFS) or
  249. (opcode = A_LES) or (opcode = A_LDS) or
  250. (opcode = A_SHR) or (opcode = A_SHL) or
  251. (opcode = A_SAR) or (opcode = A_SAL) or
  252. (opcode = A_OUT) or (opcode = A_IN)) then
  253. AsmWrite(sizestr(s,dest));
  254. WriteReference(o.ref^);
  255. end
  256. else
  257. begin
  258. asmwrite('dword ');
  259. if assigned(o.ref^.symbol) then
  260. begin
  261. asmwrite(o.ref^.symbol.name);
  262. if o.ref^.offset=0 then
  263. exit;
  264. end;
  265. if o.ref^.offset>0 then
  266. asmwrite('+');
  267. asmwrite(tostr(o.ref^.offset));
  268. end;
  269. end;
  270. else
  271. internalerror(10001);
  272. end;
  273. end;
  274. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  275. begin
  276. case o.typ of
  277. top_reg :
  278. AsmWrite(nasm_regname(o.reg));
  279. top_ref :
  280. if o.ref^.refaddr=addr_no then
  281. WriteReference(o.ref^)
  282. else
  283. begin
  284. if not(
  285. (op=A_JCXZ) or (op=A_JECXZ) or
  286. {$ifdef x86_64}
  287. (op=A_JRCXZ) or
  288. {$endif x86_64}
  289. (op=A_LOOP) or (op=A_LOOPE) or
  290. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  291. (op=A_LOOPZ)
  292. ) then
  293. AsmWrite('NEAR ');
  294. AsmWrite(o.ref^.symbol.name);
  295. if o.ref^.offset>0 then
  296. AsmWrite('+'+tostr(o.ref^.offset))
  297. else
  298. if o.ref^.offset<0 then
  299. AsmWrite(tostr(o.ref^.offset));
  300. end;
  301. top_const :
  302. AsmWrite(tostr(aint(o.val)));
  303. else
  304. internalerror(10001);
  305. end;
  306. end;
  307. var
  308. LastSecType : TAsmSectiontype;
  309. const
  310. ait_const2str : array[aitconst_128bit..aitconst_indirect_symbol] of string[20]=(
  311. #9'DDQ'#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  312. #9'FIXMESLEB',#9'FIXEMEULEB',
  313. #9'RVA'#9,#9'FIXMEINDIRECT'#9
  314. );
  315. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  316. const
  317. secnames : array[TAsmSectiontype] of string[17] = ('',
  318. '.text',
  319. '.data',
  320. '.rodata',
  321. '.bss',
  322. '.tbss',
  323. '.pdata',
  324. '.text',
  325. '.stab',
  326. '.stabstr',
  327. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  328. '.eh_frame',
  329. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  330. '.fpc',
  331. '',
  332. '.init'
  333. );
  334. begin
  335. AsmLn;
  336. AsmWrite('SECTION ');
  337. AsmWrite(secnames[atype]);
  338. if use_smartlink_section and
  339. (atype<>sec_bss) and
  340. (aname<>'') then
  341. begin
  342. AsmWrite('.');
  343. AsmWrite(aname);
  344. end;
  345. AsmLn;
  346. LasTSecType:=atype;
  347. end;
  348. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  349. var
  350. s : string;
  351. hp : tai;
  352. hp1 : tailineinfo;
  353. counter,
  354. lines,
  355. i,j,l : longint;
  356. InlineLevel : longint;
  357. consttype : taiconst_type;
  358. do_line,
  359. quoted : boolean;
  360. begin
  361. if not assigned(p) then
  362. exit;
  363. InlineLevel:=0;
  364. { lineinfo is only needed for al_procedures (PFV) }
  365. do_line:=(cs_asm_source in current_settings.globalswitches) or
  366. ((cs_lineinfo in current_settings.moduleswitches)
  367. and (p=current_asmdata.asmlists[al_procedures]));
  368. hp:=tai(p.first);
  369. while assigned(hp) do
  370. begin
  371. if not(hp.typ in SkipLineInfo) then
  372. begin
  373. hp1:=hp as tailineinfo;
  374. current_filepos:=hp1.fileinfo;
  375. if do_line then
  376. begin
  377. { load infile }
  378. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  379. begin
  380. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  381. if assigned(infile) then
  382. begin
  383. { open only if needed !! }
  384. if (cs_asm_source in current_settings.globalswitches) then
  385. infile.open;
  386. end;
  387. { avoid unnecessary reopens of the same file !! }
  388. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  389. { be sure to change line !! }
  390. lastfileinfo.line:=-1;
  391. end;
  392. { write source }
  393. if (cs_asm_source in current_settings.globalswitches) and
  394. assigned(infile) then
  395. begin
  396. if (infile<>lastinfile) then
  397. begin
  398. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  399. if assigned(lastinfile) then
  400. lastinfile.close;
  401. end;
  402. if (hp1.fileinfo.line<>lastfileinfo.line) and
  403. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  404. begin
  405. if (hp1.fileinfo.line<>0) and
  406. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  407. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  408. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  409. { set it to a negative value !
  410. to make that is has been read already !! PM }
  411. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  412. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  413. end;
  414. end;
  415. lastfileinfo:=hp1.fileinfo;
  416. lastinfile:=infile;
  417. end;
  418. end;
  419. case hp.typ of
  420. ait_comment :
  421. Begin
  422. AsmWrite(target_asm.comment);
  423. AsmWritePChar(tai_comment(hp).str);
  424. AsmLn;
  425. End;
  426. ait_regalloc :
  427. begin
  428. if (cs_asm_regalloc in current_settings.globalswitches) then
  429. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  430. regallocstr[tai_regalloc(hp).ratype]);
  431. end;
  432. ait_tempalloc :
  433. begin
  434. if (cs_asm_tempalloc in current_settings.globalswitches) then
  435. begin
  436. {$ifdef EXTDEBUG}
  437. if assigned(tai_tempalloc(hp).problem) then
  438. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  439. tostr(tai_tempalloc(hp).tempsize)+')')
  440. else
  441. {$endif EXTDEBUG}
  442. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  443. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  444. end;
  445. end;
  446. ait_section :
  447. begin
  448. if tai_section(hp).sectype<>sec_none then
  449. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  450. LasTSecType:=tai_section(hp).sectype;
  451. end;
  452. ait_align :
  453. begin
  454. { nasm gives warnings when it finds align in bss as it
  455. wants to store data }
  456. if (lastsectype<>sec_bss) and
  457. (tai_align(hp).aligntype>1) then
  458. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  459. end;
  460. ait_datablock :
  461. begin
  462. if tai_datablock(hp).is_global then
  463. begin
  464. AsmWrite(#9'GLOBAL ');
  465. AsmWriteLn(tai_datablock(hp).sym.name);
  466. end;
  467. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  468. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  469. end;
  470. ait_const:
  471. begin
  472. consttype:=tai_const(hp).consttype;
  473. case consttype of
  474. aitconst_uleb128bit,
  475. aitconst_sleb128bit,
  476. aitconst_128bit,
  477. aitconst_64bit,
  478. aitconst_32bit,
  479. aitconst_16bit,
  480. aitconst_8bit,
  481. aitconst_rva_symbol,
  482. aitconst_indirect_symbol :
  483. begin
  484. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  485. l:=0;
  486. repeat
  487. if assigned(tai_const(hp).sym) then
  488. begin
  489. if assigned(tai_const(hp).endsym) then
  490. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  491. else
  492. s:=tai_const(hp).sym.name;
  493. if tai_const(hp).value<>0 then
  494. s:=s+tostr_with_plus(tai_const(hp).value);
  495. end
  496. else
  497. s:=tostr(tai_const(hp).value);
  498. AsmWrite(s);
  499. inc(l,length(s));
  500. if (l>line_length) or
  501. (hp.next=nil) or
  502. (tai(hp.next).typ<>ait_const) or
  503. (tai_const(hp.next).consttype<>consttype) then
  504. break;
  505. hp:=tai(hp.next);
  506. AsmWrite(',');
  507. until false;
  508. AsmLn;
  509. end;
  510. end;
  511. end;
  512. ait_real_32bit :
  513. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  514. ait_real_64bit :
  515. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  516. ait_real_80bit :
  517. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  518. ait_comp_64bit :
  519. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  520. ait_string :
  521. begin
  522. counter := 0;
  523. lines := tai_string(hp).len div line_length;
  524. { separate lines in different parts }
  525. if tai_string(hp).len > 0 then
  526. Begin
  527. for j := 0 to lines-1 do
  528. begin
  529. AsmWrite(#9#9'DB'#9);
  530. quoted:=false;
  531. for i:=counter to counter+line_length-1 do
  532. begin
  533. { it is an ascii character. }
  534. if (ord(tai_string(hp).str[i])>31) and
  535. (ord(tai_string(hp).str[i])<128) and
  536. (tai_string(hp).str[i]<>'"') then
  537. begin
  538. if not(quoted) then
  539. begin
  540. if i>counter then
  541. AsmWrite(',');
  542. AsmWrite('"');
  543. end;
  544. AsmWrite(tai_string(hp).str[i]);
  545. quoted:=true;
  546. end { if > 31 and < 128 and ord('"') }
  547. else
  548. begin
  549. if quoted then
  550. AsmWrite('"');
  551. if i>counter then
  552. AsmWrite(',');
  553. quoted:=false;
  554. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  555. end;
  556. end; { end for i:=0 to... }
  557. if quoted then AsmWrite('"');
  558. AsmWrite(target_info.newline);
  559. inc(counter,line_length);
  560. end; { end for j:=0 ... }
  561. { do last line of lines }
  562. if counter<tai_string(hp).len then
  563. AsmWrite(#9#9'DB'#9);
  564. quoted:=false;
  565. for i:=counter to tai_string(hp).len-1 do
  566. begin
  567. { it is an ascii character. }
  568. if (ord(tai_string(hp).str[i])>31) and
  569. (ord(tai_string(hp).str[i])<128) and
  570. (tai_string(hp).str[i]<>'"') then
  571. begin
  572. if not(quoted) then
  573. begin
  574. if i>counter then
  575. AsmWrite(',');
  576. AsmWrite('"');
  577. end;
  578. AsmWrite(tai_string(hp).str[i]);
  579. quoted:=true;
  580. end { if > 31 and < 128 and " }
  581. else
  582. begin
  583. if quoted then
  584. AsmWrite('"');
  585. if i>counter then
  586. AsmWrite(',');
  587. quoted:=false;
  588. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  589. end;
  590. end; { end for i:=0 to... }
  591. if quoted then
  592. AsmWrite('"');
  593. end;
  594. AsmLn;
  595. end;
  596. ait_label :
  597. begin
  598. if tai_label(hp).labsym.is_used then
  599. AsmWriteLn(tai_label(hp).labsym.name+':');
  600. end;
  601. ait_symbol :
  602. begin
  603. if tai_symbol(hp).is_global then
  604. begin
  605. AsmWrite(#9'GLOBAL ');
  606. AsmWriteLn(tai_symbol(hp).sym.name);
  607. end;
  608. AsmWrite(tai_symbol(hp).sym.name);
  609. if assigned(hp.next) and not(tai(hp.next).typ in
  610. [ait_const,
  611. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  612. AsmWriteLn(':')
  613. end;
  614. ait_symbol_end : ;
  615. ait_instruction :
  616. begin
  617. taicpu(hp).CheckNonCommutativeOpcodes;
  618. { We need intel order, no At&t }
  619. taicpu(hp).SetOperandOrder(op_intel);
  620. s:='';
  621. if ((taicpu(hp).opcode=A_FADDP) or
  622. (taicpu(hp).opcode=A_FMULP))
  623. and (taicpu(hp).ops=0) then
  624. begin
  625. taicpu(hp).allocate_oper(2);
  626. taicpu(hp).oper[0]^.typ:=top_reg;
  627. taicpu(hp).oper[0]^.reg:=NR_ST1;
  628. taicpu(hp).oper[1]^.typ:=top_reg;
  629. taicpu(hp).oper[1]^.reg:=NR_ST;
  630. end;
  631. if taicpu(hp).opcode=A_FWAIT then
  632. AsmWriteln(#9#9'DB'#9'09bh')
  633. else
  634. begin
  635. { We need to explicitely set
  636. word prefix to get selectors
  637. to be pushed in 2 bytes PM }
  638. if (taicpu(hp).opsize=S_W) and
  639. ((taicpu(hp).opcode=A_PUSH) or
  640. (taicpu(hp).opcode=A_POP)) and
  641. (taicpu(hp).oper[0]^.typ=top_reg) and
  642. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  643. AsmWriteln(#9#9'DB'#9'066h');
  644. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  645. if taicpu(hp).ops<>0 then
  646. begin
  647. if is_calljmp(taicpu(hp).opcode) then
  648. begin
  649. AsmWrite(#9);
  650. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  651. end
  652. else
  653. begin
  654. for i:=0 to taicpu(hp).ops-1 do
  655. begin
  656. if i=0 then
  657. AsmWrite(#9)
  658. else
  659. AsmWrite(',');
  660. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  661. end;
  662. end;
  663. end;
  664. AsmLn;
  665. end;
  666. end;
  667. ait_stab,
  668. ait_force_line,
  669. ait_function_name : ;
  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(lasTSectype,'');
  690. AsmStartSize:=AsmSize;
  691. end;
  692. ait_marker :
  693. if tai_marker(hp).kind=mark_InlineStart then
  694. inc(InlineLevel)
  695. else if tai_marker(hp).kind=mark_InlineEnd then
  696. dec(InlineLevel);
  697. ait_directive :
  698. begin
  699. case tai_directive(hp).directive of
  700. asd_nasm_import :
  701. AsmWrite('import ');
  702. asd_extern :
  703. AsmWrite('EXTERN ');
  704. else
  705. internalerror(200509191);
  706. end;
  707. if assigned(tai_directive(hp).name) then
  708. AsmWrite(tai_directive(hp).name^);
  709. AsmLn;
  710. end;
  711. else
  712. internalerror(10000);
  713. end;
  714. hp:=tai(hp.next);
  715. end;
  716. end;
  717. procedure T386NasmAssembler.WriteExternals;
  718. var
  719. sym : TAsmSymbol;
  720. i : longint;
  721. begin
  722. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  723. begin
  724. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  725. if sym.bind=AB_EXTERNAL then
  726. AsmWriteln('EXTERN'#9+sym.name);
  727. end;
  728. end;
  729. procedure T386NasmAssembler.WriteAsmList;
  730. var
  731. hal : tasmlisttype;
  732. begin
  733. {$ifdef EXTDEBUG}
  734. if assigned(current_module.mainsource) then
  735. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  736. {$endif}
  737. LasTSecType:=sec_none;
  738. AsmWriteLn('BITS 32');
  739. AsmLn;
  740. lastfileinfo.line:=-1;
  741. lastfileinfo.fileindex:=0;
  742. lastinfile:=nil;
  743. WriteExternals;
  744. for hal:=low(TasmlistType) to high(TasmlistType) do
  745. begin
  746. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  747. writetree(current_asmdata.asmlists[hal]);
  748. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  749. end;
  750. AsmLn;
  751. {$ifdef EXTDEBUG}
  752. if assigned(current_module.mainsource) then
  753. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  754. {$endif EXTDEBUG}
  755. end;
  756. {*****************************************************************************
  757. Initialize
  758. *****************************************************************************}
  759. const
  760. as_i386_nasmcoff_info : tasminfo =
  761. (
  762. id : as_i386_nasmcoff;
  763. idtxt : 'NASMCOFF';
  764. asmbin : 'nasm';
  765. asmcmd : '-f coff -o $OBJ $ASM';
  766. supported_target : system_i386_go32v2;
  767. flags : [af_allowdirect,af_needar,af_no_debug];
  768. labelprefix : '..@';
  769. comment : '; ';
  770. );
  771. as_i386_nasmwin32_info : tasminfo =
  772. (
  773. id : as_i386_nasmwin32;
  774. idtxt : 'NASMWIN32';
  775. asmbin : 'nasm';
  776. asmcmd : '-f win32 -o $OBJ $ASM';
  777. supported_target : system_i386_win32;
  778. flags : [af_allowdirect,af_needar,af_no_debug];
  779. labelprefix : '..@';
  780. comment : '; ';
  781. );
  782. as_i386_nasmobj_info : tasminfo =
  783. (
  784. id : as_i386_nasmobj;
  785. idtxt : 'NASMOBJ';
  786. asmbin : 'nasm';
  787. asmcmd : '-f obj -o $OBJ $ASM';
  788. supported_target : system_any; { what should I write here ?? }
  789. flags : [af_allowdirect,af_needar,af_no_debug];
  790. labelprefix : '..@';
  791. comment : '; ';
  792. );
  793. as_i386_nasmwdosx_info : tasminfo =
  794. (
  795. id : as_i386_nasmwdosx;
  796. idtxt : 'NASMWDOSX';
  797. asmbin : 'nasm';
  798. asmcmd : '-f win32 -o $OBJ $ASM';
  799. supported_target : system_i386_wdosx;
  800. flags : [af_allowdirect,af_needar,af_no_debug];
  801. labelprefix : '..@';
  802. comment : '; ';
  803. );
  804. as_i386_nasmelf_info : tasminfo =
  805. (
  806. id : as_i386_nasmelf;
  807. idtxt : 'NASMELF';
  808. asmbin : 'nasm';
  809. asmcmd : '-f elf -o $OBJ $ASM';
  810. supported_target : system_i386_linux;
  811. flags : [af_allowdirect,af_needar,af_no_debug];
  812. labelprefix : '..@';
  813. comment : '; ';
  814. );
  815. as_i386_nasmbeos_info : tasminfo =
  816. (
  817. id : as_i386_nasmbeos;
  818. idtxt : 'NASMELF';
  819. asmbin : 'nasm';
  820. asmcmd : '-f elf -o $OBJ $ASM';
  821. supported_target : system_i386_beos;
  822. flags : [af_allowdirect,af_needar,af_no_debug];
  823. labelprefix : '..@';
  824. comment : '; ';
  825. );
  826. initialization
  827. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  828. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  829. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  830. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  831. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  832. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  833. end.