agx86int.pas 35 KB

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