agx86int.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for Intel syntax with Intel i386+
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {
  18. This unit implements an asmoutput class for Intel syntax with Intel i386+
  19. }
  20. unit agx86int;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cpubase,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  26. type
  27. Tx86IntelAssembler = class(TExternalAssembler)
  28. private
  29. procedure WriteReference(var ref : treference);
  30. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  31. procedure WriteOper_jmp(const o:toper;s : topsize);
  32. public
  33. procedure WriteTree(p:TAsmList);override;
  34. procedure WriteAsmList;override;
  35. Function DoAssemble:boolean;override;
  36. procedure WriteExternals;
  37. end;
  38. implementation
  39. uses
  40. SysUtils,
  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','DATA','BSS','',
  50. '','','','','','',
  51. '','','','',
  52. '',
  53. '','','','',
  54. '',
  55. '',
  56. '',
  57. '',
  58. '',
  59. '',
  60. '',
  61. '',
  62. '',
  63. '',
  64. '',
  65. '',
  66. '',
  67. '',
  68. '',
  69. '',
  70. '',
  71. '',
  72. '',
  73. '',
  74. '',
  75. '',
  76. '',
  77. '',
  78. '',
  79. '',
  80. '',
  81. '',
  82. ''
  83. );
  84. secnamesml64 : array[TAsmSectiontype] of string[7] = ('',
  85. '_TEXT','_DATE','_DATA','_DATA','_BSS','',
  86. '','','','',
  87. 'idata$2','idata$4','idata$5','idata$6','idata$7','edata',
  88. '',
  89. '','','','',
  90. '',
  91. '',
  92. '',
  93. '',
  94. '',
  95. '',
  96. '',
  97. '',
  98. '',
  99. '',
  100. '',
  101. '',
  102. '',
  103. '',
  104. '',
  105. '',
  106. '',
  107. '',
  108. '',
  109. '',
  110. '',
  111. '',
  112. '',
  113. '',
  114. '',
  115. '',
  116. '',
  117. '',
  118. ''
  119. );
  120. function single2str(d : single) : string;
  121. var
  122. hs : string;
  123. p : byte;
  124. begin
  125. str(d,hs);
  126. { nasm expects a lowercase e }
  127. p:=pos('E',hs);
  128. if p>0 then
  129. hs[p]:='e';
  130. p:=pos('+',hs);
  131. if p>0 then
  132. delete(hs,p,1);
  133. single2str:=lower(hs);
  134. end;
  135. function double2str(d : double) : string;
  136. var
  137. hs : string;
  138. p : byte;
  139. begin
  140. str(d,hs);
  141. { nasm expects a lowercase e }
  142. p:=pos('E',hs);
  143. if p>0 then
  144. hs[p]:='e';
  145. p:=pos('+',hs);
  146. if p>0 then
  147. delete(hs,p,1);
  148. double2str:=lower(hs);
  149. end;
  150. function extended2str(e : extended) : string;
  151. var
  152. hs : string;
  153. p : byte;
  154. begin
  155. str(e,hs);
  156. { nasm expects a lowercase e }
  157. p:=pos('E',hs);
  158. if p>0 then
  159. hs[p]:='e';
  160. p:=pos('+',hs);
  161. if p>0 then
  162. delete(hs,p,1);
  163. extended2str:=lower(hs);
  164. end;
  165. function comp2str(d : bestreal) : string;
  166. type
  167. pdouble = ^double;
  168. var
  169. c : comp;
  170. dd : pdouble;
  171. begin
  172. c:=comp(d);
  173. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  174. comp2str:=double2str(dd^);
  175. end;
  176. function fixline(s:string):string;
  177. {
  178. return s with all leading and ending spaces and tabs removed
  179. }
  180. var
  181. i,j,k : longint;
  182. begin
  183. i:=length(s);
  184. while (i>0) and (s[i] in [#9,' ']) do
  185. dec(i);
  186. j:=1;
  187. while (j<i) and (s[j] in [#9,' ']) do
  188. inc(j);
  189. for k:=j to i do
  190. if s[k] in [#0..#31,#127..#255] then
  191. s[k]:='.';
  192. fixline:=Copy(s,j,i-j+1);
  193. end;
  194. {****************************************************************************
  195. tx86IntelAssembler
  196. ****************************************************************************}
  197. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  198. var
  199. first : boolean;
  200. begin
  201. with ref do
  202. begin
  203. first:=true;
  204. if segment<>NR_NO then
  205. AsmWrite(masm_regname(segment)+':[')
  206. else
  207. AsmWrite('[');
  208. if assigned(symbol) then
  209. begin
  210. if (target_asm.id = as_i386_tasm) then
  211. AsmWrite('dword ptr ');
  212. AsmWrite(symbol.name);
  213. first:=false;
  214. end;
  215. if (base<>NR_NO) then
  216. begin
  217. if not(first) then
  218. AsmWrite('+')
  219. else
  220. first:=false;
  221. AsmWrite(masm_regname(base));
  222. end;
  223. if (index<>NR_NO) then
  224. begin
  225. if not(first) then
  226. AsmWrite('+')
  227. else
  228. first:=false;
  229. AsmWrite(masm_regname(index));
  230. if scalefactor<>0 then
  231. AsmWrite('*'+tostr(scalefactor));
  232. end;
  233. if offset<0 then
  234. begin
  235. AsmWrite(tostr(offset));
  236. first:=false;
  237. end
  238. else if (offset>0) then
  239. begin
  240. AsmWrite('+'+tostr(offset));
  241. first:=false;
  242. end;
  243. if first then
  244. AsmWrite('0');
  245. AsmWrite(']');
  246. end;
  247. end;
  248. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  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. begin
  257. if o.ref^.refaddr=addr_no then
  258. begin
  259. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  260. (opcode <> A_LFS) and (opcode <> A_LDS) and
  261. (opcode <> A_LES)) then
  262. Begin
  263. case s of
  264. S_B : AsmWrite('byte ptr ');
  265. S_W : AsmWrite('word ptr ');
  266. S_L : AsmWrite('dword ptr ');
  267. S_Q : AsmWrite('qword ptr ');
  268. S_IS : AsmWrite('word ptr ');
  269. S_IL : AsmWrite('dword ptr ');
  270. S_IQ : AsmWrite('qword ptr ');
  271. S_FS : AsmWrite('dword ptr ');
  272. S_FL : AsmWrite('qword ptr ');
  273. S_T,
  274. S_FX : AsmWrite('tbyte ptr ');
  275. S_BW : if dest then
  276. AsmWrite('word ptr ')
  277. else
  278. AsmWrite('byte ptr ');
  279. S_BL : if dest then
  280. AsmWrite('dword ptr ')
  281. else
  282. AsmWrite('byte ptr ');
  283. S_WL : if dest then
  284. AsmWrite('dword ptr ')
  285. else
  286. AsmWrite('word ptr ');
  287. {$ifdef x86_64}
  288. S_BQ : if dest then
  289. AsmWrite('qword ptr ')
  290. else
  291. AsmWrite('byte ptr ');
  292. S_WQ : if dest then
  293. AsmWrite('qword ptr ')
  294. else
  295. AsmWrite('word ptr ');
  296. S_LQ : if dest then
  297. AsmWrite('qword ptr ')
  298. else
  299. AsmWrite('dword ptr ');
  300. S_XMM: AsmWrite('xmmword ptr ');
  301. {$endif x86_64}
  302. end;
  303. end;
  304. WriteReference(o.ref^);
  305. end
  306. else
  307. begin
  308. AsmWrite('offset ');
  309. if assigned(o.ref^.symbol) then
  310. AsmWrite(o.ref^.symbol.name);
  311. if o.ref^.offset>0 then
  312. AsmWrite('+'+tostr(o.ref^.offset))
  313. else
  314. if o.ref^.offset<0 then
  315. AsmWrite(tostr(o.ref^.offset))
  316. else
  317. if not(assigned(o.ref^.symbol)) then
  318. AsmWrite('0');
  319. end;
  320. end;
  321. else
  322. internalerror(2005060510);
  323. end;
  324. end;
  325. procedure tx86IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  326. begin
  327. case o.typ of
  328. top_reg :
  329. AsmWrite(masm_regname(o.reg));
  330. top_const :
  331. AsmWrite(tostr(longint(o.val)));
  332. top_ref :
  333. { what about lcall or ljmp ??? }
  334. begin
  335. if o.ref^.refaddr=addr_no then
  336. begin
  337. if (target_asm.id <> as_i386_tasm) then
  338. begin
  339. if s=S_FAR then
  340. AsmWrite('far ptr ')
  341. else
  342. {$ifdef x86_64}
  343. AsmWrite('qword ptr ');
  344. {$else x86_64}
  345. AsmWrite('dword ptr ');
  346. {$endif x86_64}
  347. end;
  348. WriteReference(o.ref^);
  349. end
  350. else
  351. begin
  352. AsmWrite(o.ref^.symbol.name);
  353. if o.ref^.offset>0 then
  354. AsmWrite('+'+tostr(o.ref^.offset))
  355. else
  356. if o.ref^.offset<0 then
  357. AsmWrite(tostr(o.ref^.offset));
  358. end;
  359. end;
  360. else
  361. internalerror(2005060511);
  362. end;
  363. end;
  364. const
  365. ait_const2str : array[aitconst_128bit..aitconst_indirect_symbol] of string[20]=(
  366. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  367. #9'FIXMESLEB',#9'FIXEMEULEB',
  368. #9'DD RVA'#9,#9'DD SECREL32'#9,#9'FIXMEINDIRECT'#9
  369. );
  370. Function PadTabs(const p:string;addch:char):string;
  371. var
  372. s : string;
  373. i : longint;
  374. begin
  375. i:=length(p);
  376. if addch<>#0 then
  377. begin
  378. inc(i);
  379. s:=p+addch;
  380. end
  381. else
  382. s:=p;
  383. if i<8 then
  384. PadTabs:=s+#9#9
  385. else
  386. PadTabs:=s+#9;
  387. end;
  388. procedure tx86IntelAssembler.WriteTree(p:TAsmList);
  389. const
  390. regallocstr : array[tregalloctype] of string[10]=(' allocated',' released',' sync',' resized');
  391. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  392. var
  393. s,
  394. prefix,
  395. suffix : string;
  396. hp : tai;
  397. hp1 : tailineinfo;
  398. counter,
  399. lines,
  400. InlineLevel : longint;
  401. i,j,l : longint;
  402. consttype : taiconst_type;
  403. do_line,DoNotSplitLine,
  404. quoted : boolean;
  405. begin
  406. if not assigned(p) then
  407. exit;
  408. { lineinfo is only needed for al_procedures (PFV) }
  409. do_line:=((cs_asm_source in current_settings.globalswitches) or
  410. (cs_lineinfo in current_settings.moduleswitches))
  411. and (p=current_asmdata.asmlists[al_procedures]);
  412. InlineLevel:=0;
  413. DoNotSplitLine:=false;
  414. hp:=tai(p.first);
  415. while assigned(hp) do
  416. begin
  417. if do_line and not(hp.typ in SkipLineInfo) and
  418. not DoNotSplitLine then
  419. begin
  420. hp1:=hp as tailineinfo;
  421. { load infile }
  422. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  423. begin
  424. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  425. if assigned(infile) then
  426. begin
  427. { open only if needed !! }
  428. if (cs_asm_source in current_settings.globalswitches) then
  429. infile.open;
  430. end;
  431. { avoid unnecessary reopens of the same file !! }
  432. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  433. { be sure to change line !! }
  434. lastfileinfo.line:=-1;
  435. end;
  436. { write source }
  437. if (cs_asm_source in current_settings.globalswitches) and
  438. assigned(infile) then
  439. begin
  440. if (infile<>lastinfile) then
  441. begin
  442. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  443. if assigned(lastinfile) then
  444. lastinfile.close;
  445. end;
  446. if (hp1.fileinfo.line<>lastfileinfo.line) and
  447. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  448. begin
  449. if (hp1.fileinfo.line<>0) and
  450. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  451. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  452. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  453. { set it to a negative value !
  454. to make that is has been read already !! PM }
  455. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  456. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  457. end;
  458. end;
  459. lastfileinfo:=hp1.fileinfo;
  460. lastinfile:=infile;
  461. end;
  462. DoNotSplitLine:=false;
  463. case hp.typ of
  464. ait_comment :
  465. Begin
  466. AsmWrite(target_asm.comment);
  467. AsmWritePChar(tai_comment(hp).str);
  468. AsmLn;
  469. End;
  470. ait_regalloc :
  471. begin
  472. if (cs_asm_regalloc in current_settings.globalswitches) then
  473. AsmWriteLn(target_asm.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  474. regallocstr[tai_regalloc(hp).ratype]);
  475. end;
  476. ait_tempalloc :
  477. begin
  478. if (cs_asm_tempalloc in current_settings.globalswitches) then
  479. begin
  480. {$ifdef EXTDEBUG}
  481. if assigned(tai_tempalloc(hp).problem) then
  482. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  483. tostr(tai_tempalloc(hp).tempsize)+')')
  484. else
  485. {$endif EXTDEBUG}
  486. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  487. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  488. end;
  489. end;
  490. ait_section :
  491. begin
  492. if tai_section(hp).sectype<>sec_none then
  493. begin
  494. if target_asm.id=as_x86_64_masm then
  495. begin
  496. if LasTSecType<>sec_none then
  497. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  498. AsmLn;
  499. AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  500. end
  501. else
  502. begin
  503. if LasTSecType<>sec_none then
  504. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  505. AsmLn;
  506. AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  507. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  508. secnames[tai_section(hp).sectype]+'''');
  509. end;
  510. end;
  511. LasTSecType:=tai_section(hp).sectype;
  512. end;
  513. ait_align :
  514. begin
  515. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  516. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  517. { HERE UNDER TASM! }
  518. if tai_align_abstract(hp).aligntype>1 then
  519. AsmWriteLn(#9'ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  520. end;
  521. ait_datablock :
  522. begin
  523. if tai_datablock(hp).is_global then
  524. AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  525. AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  526. end;
  527. ait_const:
  528. begin
  529. consttype:=tai_const(hp).consttype;
  530. case consttype of
  531. aitconst_uleb128bit,
  532. aitconst_sleb128bit,
  533. aitconst_128bit,
  534. aitconst_64bit,
  535. aitconst_32bit,
  536. aitconst_16bit,
  537. aitconst_8bit,
  538. aitconst_rva_symbol,
  539. aitconst_secrel32_symbol,
  540. aitconst_indirect_symbol :
  541. begin
  542. AsmWrite(ait_const2str[consttype]);
  543. l:=0;
  544. repeat
  545. if assigned(tai_const(hp).sym) then
  546. begin
  547. if assigned(tai_const(hp).endsym) then
  548. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  549. else
  550. s:=tai_const(hp).sym.name;
  551. if tai_const(hp).value<>0 then
  552. s:=s+tostr_with_plus(tai_const(hp).value);
  553. end
  554. else
  555. s:=tostr(tai_const(hp).value);
  556. AsmWrite(s);
  557. inc(l,length(s));
  558. if (l>line_length) or
  559. (hp.next=nil) or
  560. (tai(hp.next).typ<>ait_const) or
  561. (tai_const(hp.next).consttype<>consttype) then
  562. break;
  563. hp:=tai(hp.next);
  564. AsmWrite(',');
  565. until false;
  566. AsmLn;
  567. end;
  568. else
  569. internalerror(200704253);
  570. end;
  571. end;
  572. ait_real_32bit :
  573. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  574. ait_real_64bit :
  575. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  576. ait_real_80bit :
  577. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  578. ait_comp_64bit :
  579. AsmWriteLn(#9#9'DQ'#9+extended2str(tai_comp_64bit(hp).value));
  580. ait_string :
  581. begin
  582. counter := 0;
  583. lines := tai_string(hp).len div line_length;
  584. { separate lines in different parts }
  585. if tai_string(hp).len > 0 then
  586. Begin
  587. for j := 0 to lines-1 do
  588. begin
  589. AsmWrite(#9#9'DB'#9);
  590. quoted:=false;
  591. for i:=counter to counter+line_length-1 do
  592. begin
  593. { it is an ascii character. }
  594. if (ord(tai_string(hp).str[i])>31) and
  595. (ord(tai_string(hp).str[i])<128) and
  596. (tai_string(hp).str[i]<>'"') then
  597. begin
  598. if not(quoted) then
  599. begin
  600. if i>counter then
  601. AsmWrite(',');
  602. AsmWrite('"');
  603. end;
  604. AsmWrite(tai_string(hp).str[i]);
  605. quoted:=true;
  606. end { if > 31 and < 128 and ord('"') }
  607. else
  608. begin
  609. if quoted then
  610. AsmWrite('"');
  611. if i>counter then
  612. AsmWrite(',');
  613. quoted:=false;
  614. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  615. end;
  616. end; { end for i:=0 to... }
  617. if quoted then AsmWrite('"');
  618. AsmWrite(target_info.newline);
  619. counter := counter+line_length;
  620. end; { end for j:=0 ... }
  621. { do last line of lines }
  622. if counter<tai_string(hp).len then
  623. AsmWrite(#9#9'DB'#9);
  624. quoted:=false;
  625. for i:=counter to tai_string(hp).len-1 do
  626. begin
  627. { it is an ascii character. }
  628. if (ord(tai_string(hp).str[i])>31) and
  629. (ord(tai_string(hp).str[i])<128) and
  630. (tai_string(hp).str[i]<>'"') then
  631. begin
  632. if not(quoted) then
  633. begin
  634. if i>counter then
  635. AsmWrite(',');
  636. AsmWrite('"');
  637. end;
  638. AsmWrite(tai_string(hp).str[i]);
  639. quoted:=true;
  640. end { if > 31 and < 128 and " }
  641. else
  642. begin
  643. if quoted then
  644. AsmWrite('"');
  645. if i>counter then
  646. AsmWrite(',');
  647. quoted:=false;
  648. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  649. end;
  650. end; { end for i:=0 to... }
  651. if quoted then
  652. AsmWrite('"');
  653. end;
  654. AsmLn;
  655. end;
  656. ait_label :
  657. begin
  658. if tai_label(hp).labsym.is_used then
  659. begin
  660. AsmWrite(tai_label(hp).labsym.name);
  661. if assigned(hp.next) and not(tai(hp.next).typ in
  662. [ait_const,
  663. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  664. AsmWriteLn(':')
  665. else
  666. DoNotSplitLine:=true;
  667. end;
  668. end;
  669. ait_symbol :
  670. begin
  671. if tai_symbol(hp).is_global then
  672. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  673. AsmWrite(tai_symbol(hp).sym.name);
  674. if assigned(hp.next) and not(tai(hp.next).typ in
  675. [ait_const,
  676. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  677. AsmWriteLn(':')
  678. end;
  679. ait_symbol_end :
  680. begin
  681. end;
  682. ait_instruction :
  683. begin
  684. taicpu(hp).CheckNonCommutativeOpcodes;
  685. taicpu(hp).SetOperandOrder(op_intel);
  686. { Reset }
  687. suffix:='';
  688. prefix:= '';
  689. { We need to explicitely set
  690. word prefix to get selectors
  691. to be pushed in 2 bytes PM }
  692. if (taicpu(hp).opsize=S_W) and
  693. (
  694. (
  695. (taicpu(hp).opcode=A_PUSH) or
  696. (taicpu(hp).opcode=A_POP)
  697. ) and
  698. (taicpu(hp).oper[0]^.typ=top_reg) and
  699. is_segment_reg(taicpu(hp).oper[0]^.reg)
  700. ) then
  701. AsmWriteln(#9#9'DB'#9'066h');
  702. { added prefix instructions, must be on same line as opcode }
  703. if (taicpu(hp).ops = 0) and
  704. ((taicpu(hp).opcode = A_REP) or
  705. (taicpu(hp).opcode = A_LOCK) or
  706. (taicpu(hp).opcode = A_REPE) or
  707. (taicpu(hp).opcode = A_REPNZ) or
  708. (taicpu(hp).opcode = A_REPZ) or
  709. (taicpu(hp).opcode = A_REPNE)) then
  710. Begin
  711. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  712. { there can be a stab inbetween when the opcode was on
  713. a different line in the source code }
  714. repeat
  715. hp:=tai(hp.next);
  716. until (hp=nil) or (hp.typ=ait_instruction);
  717. { this is theorically impossible... }
  718. if hp=nil then
  719. begin
  720. AsmWriteLn(#9#9+prefix);
  721. break;
  722. end;
  723. { nasm prefers prefix on a line alone
  724. AsmWriteln(#9#9+prefix); but not masm PM
  725. prefix:=''; }
  726. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  727. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  728. begin
  729. AsmWriteln(prefix);
  730. prefix:='';
  731. end;
  732. end
  733. else
  734. prefix:= '';
  735. if (target_asm.id = as_i386_wasm) and
  736. (taicpu(hp).opsize=S_W) and
  737. (taicpu(hp).opcode=A_PUSH) and
  738. (taicpu(hp).oper[0]^.typ=top_const) then
  739. begin
  740. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  741. AsmWrite(#9#9'DW');
  742. end
  743. else if (target_asm.id=as_x86_64_masm) and
  744. (taicpu(hp).opcode=A_MOVQ) then
  745. AsmWrite(#9#9'mov')
  746. else
  747. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  748. if taicpu(hp).ops<>0 then
  749. begin
  750. if is_calljmp(taicpu(hp).opcode) then
  751. begin
  752. AsmWrite(#9);
  753. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  754. end
  755. else
  756. begin
  757. for i:=0to taicpu(hp).ops-1 do
  758. begin
  759. if i=0 then
  760. AsmWrite(#9)
  761. else
  762. AsmWrite(',');
  763. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  764. end;
  765. end;
  766. end;
  767. AsmLn;
  768. end;
  769. ait_stab,
  770. ait_force_line,
  771. ait_function_name : ;
  772. ait_cutobject :
  773. begin
  774. { only reset buffer if nothing has changed }
  775. if AsmSize=AsmStartSize then
  776. AsmClear
  777. else
  778. begin
  779. if LasTSecType<>sec_none then
  780. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  781. AsmLn;
  782. AsmWriteLn(#9'END');
  783. AsmClose;
  784. DoAssemble;
  785. AsmCreate(tai_cutobject(hp).place);
  786. end;
  787. { avoid empty files }
  788. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  789. begin
  790. if tai(hp.next).typ=ait_section then
  791. lasTSecType:=tai_section(hp.next).sectype;
  792. hp:=tai(hp.next);
  793. end;
  794. AsmWriteLn(#9'.386p');
  795. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  796. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  797. { I was told that this isn't necesarry because }
  798. { the labels generated by FPC are unique (FK) }
  799. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  800. if lasTSectype<>sec_none then
  801. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  802. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  803. secnames[lasTSectype]+'''');
  804. AsmStartSize:=AsmSize;
  805. end;
  806. ait_marker :
  807. begin
  808. if tai_marker(hp).kind=mark_InlineStart then
  809. inc(InlineLevel)
  810. else if tai_marker(hp).kind=mark_InlineEnd then
  811. dec(InlineLevel);
  812. end;
  813. ait_directive :
  814. begin
  815. case tai_directive(hp).directive of
  816. asd_nasm_import :
  817. AsmWrite('import ');
  818. asd_extern :
  819. AsmWrite('EXTRN ');
  820. else
  821. internalerror(200509192);
  822. end;
  823. if assigned(tai_directive(hp).name) then
  824. AsmWrite(tai_directive(hp).name^);
  825. AsmLn;
  826. end;
  827. else
  828. internalerror(10000);
  829. end;
  830. hp:=tai(hp.next);
  831. end;
  832. end;
  833. procedure tx86intelassembler.WriteExternals;
  834. var
  835. sym : TAsmSymbol;
  836. i : longint;
  837. begin
  838. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  839. begin
  840. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  841. if sym.bind=AB_EXTERNAL then
  842. begin
  843. case target_asm.id of
  844. as_i386_masm,
  845. as_i386_wasm :
  846. AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  847. as_x86_64_masm :
  848. AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  849. else
  850. AsmWriteln(#9'EXTRN'#9+sym.name);
  851. end;
  852. end;
  853. end;
  854. end;
  855. function tx86intelassembler.DoAssemble : boolean;
  856. var
  857. masmobjfn : string;
  858. begin
  859. DoAssemble:=Inherited DoAssemble;
  860. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  861. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  862. begin
  863. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  864. if not(cs_asm_extern in current_settings.globalswitches) then
  865. begin
  866. if Not FileExists(objfilename) and
  867. FileExists(masmobjfn) then
  868. RenameFile(masmobjfn,objfilename);
  869. end
  870. else
  871. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  872. end;
  873. end;
  874. procedure tx86IntelAssembler.WriteAsmList;
  875. var
  876. hal : tasmlisttype;
  877. begin
  878. {$ifdef EXTDEBUG}
  879. if assigned(current_module.mainsource) then
  880. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  881. {$endif}
  882. if target_asm.id<>as_x86_64_masm then
  883. begin
  884. AsmWriteLn(#9'.386p');
  885. { masm 6.11 does not seem to like LOCALS PM }
  886. if (target_asm.id = as_i386_tasm) then
  887. begin
  888. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  889. end;
  890. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  891. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  892. AsmLn;
  893. end;
  894. WriteExternals;
  895. for hal:=low(TasmlistType) to high(TasmlistType) do
  896. begin
  897. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  898. writetree(current_asmdata.asmlists[hal]);
  899. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  900. end;
  901. { better do this at end of WriteTree, but then there comes a trouble with
  902. al_const which does not have leading ait_section and thus goes out of segment }
  903. { TODO: probably ml64 needs 'closing' last section, too }
  904. if LastSecType <> sec_none then
  905. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  906. LastSecType := sec_none;
  907. AsmWriteLn(#9'END');
  908. AsmLn;
  909. {$ifdef EXTDEBUG}
  910. if assigned(current_module.mainsource) then
  911. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  912. {$endif EXTDEBUG}
  913. end;
  914. {*****************************************************************************
  915. Initialize
  916. *****************************************************************************}
  917. const
  918. {$ifdef i386}
  919. as_i386_tasm_info : tasminfo =
  920. (
  921. id : as_i386_tasm;
  922. idtxt : 'TASM';
  923. asmbin : 'tasm';
  924. asmcmd : '/m2 /ml $ASM $OBJ';
  925. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  926. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  927. labelprefix : '@@';
  928. comment : '; ';
  929. );
  930. as_i386_masm_info : tasminfo =
  931. (
  932. id : as_i386_masm;
  933. idtxt : 'MASM';
  934. asmbin : 'masm';
  935. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  936. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  937. flags : [af_allowdirect,af_needar];
  938. labelprefix : '@@';
  939. comment : '; ';
  940. );
  941. as_i386_wasm_info : tasminfo =
  942. (
  943. id : as_i386_wasm;
  944. idtxt : 'WASM';
  945. asmbin : 'wasm';
  946. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  947. supported_targets : [system_i386_watcom];
  948. flags : [af_allowdirect,af_needar];
  949. labelprefix : '@@';
  950. comment : '; ';
  951. );
  952. {$endif i386}
  953. {$ifdef x86_64}
  954. as_x86_64_masm_info : tasminfo =
  955. (
  956. id : as_x86_64_masm;
  957. idtxt : 'MASM';
  958. asmbin : 'ml64';
  959. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  960. supported_targets : [system_x86_64_win64];
  961. flags : [af_allowdirect,af_needar];
  962. labelprefix : '@@';
  963. comment : '; ';
  964. );
  965. {$endif x86_64}
  966. initialization
  967. {$ifdef x86_64}
  968. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  969. {$endif x86_64}
  970. {$ifdef i386}
  971. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  972. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  973. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  974. {$endif i386}
  975. end.