ag386int.pas 33 KB

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