ag386int.pas 32 KB

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