2
0

agx86int.pas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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,constexp,
  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. function single2str(d : single) : string; override;
  34. function double2str(d : double) : string; override;
  35. function extended2str(e : extended) : string; override;
  36. function comp2str(d : bestreal) : string;
  37. procedure WriteTree(p:TAsmList);override;
  38. procedure WriteAsmList;override;
  39. Function DoAssemble:boolean;override;
  40. procedure WriteExternals;
  41. end;
  42. implementation
  43. uses
  44. SysUtils,
  45. cutils,globtype,globals,systems,cclasses,
  46. verbose,cscript,cpuinfo,
  47. itx86int,
  48. cgbase
  49. {$ifdef EXTDEBUG}
  50. ,fmodule
  51. {$endif EXTDEBUG}
  52. ;
  53. const
  54. line_length = 70;
  55. wasm_cpu_name : array[tcputype] of string = (
  56. {$if defined(x86_64)}
  57. 'IA64', // cpu_none,
  58. '686', // cpu_athlon64,
  59. '686', // cpu_core_i,
  60. '686', // cpu_core_avx,
  61. '686' // cpu_core_avx2
  62. {$elseif defined(i386)}
  63. 'IA64', // cpu_none,
  64. '386', // cpu_386,
  65. '486', // cpu_486,
  66. '586', // cpu_Pentium,
  67. '686', // cpu_Pentium2,
  68. '686', // cpu_Pentium3,
  69. '686', // cpu_Pentium4,
  70. '686', // cpu_PentiumM,
  71. '686', // cpu_core_i,
  72. '686', // cpu_core_avx,
  73. '686' // cpu_core_avx2
  74. {$elseif defined(i8086)}
  75. 'IA64', // cpu_none
  76. '8086', // cpu_8086
  77. '186', // cpu_186
  78. '286', // cpu_286
  79. '386', // cpu_386
  80. '486', // cpu_486
  81. '586', // cpu_Pentium
  82. '686', // cpu_Pentium2
  83. '686', // cpu_Pentium3
  84. '686', // cpu_Pentium4
  85. '686' // cpu_PentiumM
  86. {$endif}
  87. );
  88. secnames : array[TAsmSectiontype] of string[4] = ('','',
  89. 'CODE','DATA','DATA','DATA','BSS','TLS',
  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. '',
  121. '',
  122. '',
  123. '',
  124. '',
  125. '',
  126. '',
  127. '',
  128. '',
  129. '',
  130. '',
  131. '',
  132. '',
  133. '',
  134. '',
  135. '',
  136. '',
  137. ''
  138. );
  139. secnamesml64 : array[TAsmSectiontype] of string[7] = ('','',
  140. '_TEXT','_DATA','_DATA','_DATA','_BSS','_TLS',
  141. '','','','',
  142. 'idata$2','idata$4','idata$5','idata$6','idata$7','edata',
  143. '',
  144. '',
  145. '',
  146. '',
  147. '',
  148. '','','','','','',
  149. '',
  150. '',
  151. '',
  152. '',
  153. '',
  154. '',
  155. '',
  156. '',
  157. '',
  158. '',
  159. '',
  160. '',
  161. '',
  162. '',
  163. '',
  164. '',
  165. '',
  166. '',
  167. '',
  168. '',
  169. '',
  170. '',
  171. '',
  172. '',
  173. '',
  174. '',
  175. '',
  176. '',
  177. '',
  178. '',
  179. '',
  180. '',
  181. '',
  182. '',
  183. '',
  184. '',
  185. '',
  186. '',
  187. '',
  188. ''
  189. );
  190. function TX86IntelAssembler.single2str(d : single) : string;
  191. var
  192. hs : string;
  193. p : byte;
  194. begin
  195. str(d,hs);
  196. { nasm expects a lowercase e }
  197. p:=pos('E',hs);
  198. if p>0 then
  199. hs[p]:='e';
  200. p:=pos('+',hs);
  201. if p>0 then
  202. delete(hs,p,1);
  203. single2str:=lower(hs);
  204. end;
  205. function TX86IntelAssembler.double2str(d : double) : string;
  206. var
  207. hs : string;
  208. p : byte;
  209. begin
  210. str(d,hs);
  211. { nasm expects a lowercase e }
  212. p:=pos('E',hs);
  213. if p>0 then
  214. hs[p]:='e';
  215. p:=pos('+',hs);
  216. if p>0 then
  217. delete(hs,p,1);
  218. double2str:=lower(hs);
  219. end;
  220. function TX86IntelAssembler.extended2str(e : extended) : string;
  221. var
  222. hs : string;
  223. p : byte;
  224. begin
  225. str(e,hs);
  226. { nasm expects a lowercase e }
  227. p:=pos('E',hs);
  228. if p>0 then
  229. hs[p]:='e';
  230. p:=pos('+',hs);
  231. if p>0 then
  232. delete(hs,p,1);
  233. extended2str:=lower(hs);
  234. end;
  235. function TX86IntelAssembler.comp2str(d : bestreal) : string;
  236. type
  237. pdouble = ^double;
  238. var
  239. c : comp;
  240. dd : pdouble;
  241. begin
  242. c:=comp(d);
  243. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  244. comp2str:=double2str(dd^);
  245. end;
  246. { MASM supports aligns up to 8192 }
  247. function alignstr(b : longint) : string;
  248. begin
  249. case b of
  250. 1: result:='BYTE';
  251. 2: result:='WORD';
  252. 4: result:='DWORD';
  253. 0,
  254. 16: result:='PARA';
  255. 256: result:='PAGE';
  256. else
  257. result:='ALIGN('+tostr(b)+')';
  258. end;
  259. end;
  260. {****************************************************************************
  261. tx86IntelAssembler
  262. ****************************************************************************}
  263. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  264. var
  265. first : boolean;
  266. begin
  267. with ref do
  268. begin
  269. first:=true;
  270. if segment<>NR_NO then
  271. writer.AsmWrite(masm_regname(segment)+':[')
  272. else
  273. writer.AsmWrite('[');
  274. if assigned(symbol) then
  275. begin
  276. if (asminfo^.id = as_i386_tasm) then
  277. writer.AsmWrite('dword ptr ');
  278. writer.AsmWrite(symbol.name);
  279. first:=false;
  280. end;
  281. if (base<>NR_NO) then
  282. begin
  283. if not(first) then
  284. writer.AsmWrite('+')
  285. else
  286. first:=false;
  287. {$ifdef x86_64}
  288. { ml64 needs [$+foo] instead of [rip+foo] }
  289. if (base=NR_RIP) and (asminfo^.id=as_x86_64_masm) then
  290. writer.AsmWrite('$')
  291. else
  292. {$endif x86_64}
  293. writer.AsmWrite(masm_regname(base));
  294. end;
  295. if (index<>NR_NO) then
  296. begin
  297. if not(first) then
  298. writer.AsmWrite('+')
  299. else
  300. first:=false;
  301. writer.AsmWrite(masm_regname(index));
  302. if scalefactor<>0 then
  303. writer.AsmWrite('*'+tostr(scalefactor));
  304. end;
  305. if offset<0 then
  306. begin
  307. writer.AsmWrite(tostr(offset));
  308. first:=false;
  309. end
  310. else if (offset>0) then
  311. begin
  312. writer.AsmWrite('+'+tostr(offset));
  313. first:=false;
  314. end;
  315. if first then
  316. writer.AsmWrite('0');
  317. writer.AsmWrite(']');
  318. end;
  319. end;
  320. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  321. begin
  322. case o.typ of
  323. top_reg :
  324. writer.AsmWrite(masm_regname(o.reg));
  325. top_const :
  326. writer.AsmWrite(tostr(o.val));
  327. top_ref :
  328. begin
  329. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  330. begin
  331. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  332. (opcode <> A_LFS)
  333. {$ifndef x86_64}
  334. and (opcode <> A_LDS) and (opcode <> A_LES)
  335. {$endif x86_64}
  336. ) then
  337. Begin
  338. case s of
  339. S_B : writer.AsmWrite('byte ptr ');
  340. S_W : writer.AsmWrite('word ptr ');
  341. S_L : writer.AsmWrite('dword ptr ');
  342. S_Q : writer.AsmWrite('qword ptr ');
  343. S_IS : writer.AsmWrite('word ptr ');
  344. S_IL : writer.AsmWrite('dword ptr ');
  345. S_IQ : writer.AsmWrite('qword ptr ');
  346. S_FS : writer.AsmWrite('dword ptr ');
  347. S_FL : writer.AsmWrite('qword ptr ');
  348. S_T,
  349. S_FX : writer.AsmWrite('tbyte ptr ');
  350. S_BW : if dest then
  351. writer.AsmWrite('word ptr ')
  352. else
  353. writer.AsmWrite('byte ptr ');
  354. S_BL : if dest then
  355. writer.AsmWrite('dword ptr ')
  356. else
  357. writer.AsmWrite('byte ptr ');
  358. S_WL : if dest then
  359. writer.AsmWrite('dword ptr ')
  360. else
  361. writer.AsmWrite('word ptr ');
  362. S_XMM: writer.AsmWrite('xmmword ptr ');
  363. S_YMM: writer.AsmWrite('ymmword ptr ');
  364. {$ifdef x86_64}
  365. S_BQ : if dest then
  366. writer.AsmWrite('qword ptr ')
  367. else
  368. writer.AsmWrite('byte ptr ');
  369. S_WQ : if dest then
  370. writer.AsmWrite('qword ptr ')
  371. else
  372. writer.AsmWrite('word ptr ');
  373. S_LQ : if dest then
  374. writer.AsmWrite('qword ptr ')
  375. else
  376. writer.AsmWrite('dword ptr ');
  377. {$endif x86_64}
  378. end;
  379. end;
  380. WriteReference(o.ref^);
  381. end
  382. else
  383. begin
  384. writer.AsmWrite('offset ');
  385. if assigned(o.ref^.symbol) then
  386. writer.AsmWrite(o.ref^.symbol.name);
  387. if o.ref^.offset>0 then
  388. writer.AsmWrite('+'+tostr(o.ref^.offset))
  389. else
  390. if o.ref^.offset<0 then
  391. writer.AsmWrite(tostr(o.ref^.offset))
  392. else
  393. if not(assigned(o.ref^.symbol)) then
  394. writer.AsmWrite('0');
  395. end;
  396. end;
  397. else
  398. internalerror(2005060510);
  399. end;
  400. end;
  401. procedure tx86IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  402. begin
  403. case o.typ of
  404. top_reg :
  405. writer.AsmWrite(masm_regname(o.reg));
  406. top_const :
  407. writer.AsmWrite(tostr(o.val));
  408. top_ref :
  409. { what about lcall or ljmp ??? }
  410. begin
  411. if o.ref^.refaddr=addr_no then
  412. begin
  413. if (asminfo^.id <> as_i386_tasm) then
  414. begin
  415. if s=S_FAR then
  416. writer.AsmWrite('far ptr ')
  417. else
  418. {$ifdef x86_64}
  419. writer.AsmWrite('qword ptr ');
  420. {$else x86_64}
  421. writer.AsmWrite('dword ptr ');
  422. {$endif x86_64}
  423. end;
  424. WriteReference(o.ref^);
  425. end
  426. else
  427. begin
  428. writer.AsmWrite(o.ref^.symbol.name);
  429. if o.ref^.offset>0 then
  430. writer.AsmWrite('+'+tostr(o.ref^.offset))
  431. else
  432. if o.ref^.offset<0 then
  433. writer.AsmWrite(tostr(o.ref^.offset));
  434. end;
  435. end;
  436. else
  437. internalerror(2005060511);
  438. end;
  439. end;
  440. const
  441. ait_const2str : array[aitconst_128bit..aitconst_secrel32_symbol] of string[20]=(
  442. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  443. #9'FIXMESLEB',#9'FIXEMEULEB',
  444. #9'DD RVA'#9,#9'DD SECREL32'#9
  445. );
  446. Function PadTabs(const p:string;addch:char):string;
  447. var
  448. s : string;
  449. i : longint;
  450. begin
  451. i:=length(p);
  452. if addch<>#0 then
  453. begin
  454. inc(i);
  455. s:=p+addch;
  456. end
  457. else
  458. s:=p;
  459. if i<8 then
  460. PadTabs:=s+#9#9
  461. else
  462. PadTabs:=s+#9;
  463. end;
  464. procedure tx86IntelAssembler.WriteTree(p:TAsmList);
  465. var
  466. s,
  467. prefix,
  468. suffix : string;
  469. hp,nhp : tai;
  470. cpu: tcputype;
  471. counter,
  472. lines,
  473. InlineLevel : longint;
  474. i,j,l : longint;
  475. consttype : taiconst_type;
  476. do_line,DoNotSplitLine,
  477. quoted : boolean;
  478. fixed_opcode: TAsmOp;
  479. begin
  480. if not assigned(p) then
  481. exit;
  482. { lineinfo is only needed for al_procedures (PFV) }
  483. do_line:=((cs_asm_source in current_settings.globalswitches) or
  484. (cs_lineinfo in current_settings.moduleswitches))
  485. and (p=current_asmdata.asmlists[al_procedures]);
  486. InlineLevel:=0;
  487. DoNotSplitLine:=false;
  488. hp:=tai(p.first);
  489. while assigned(hp) do
  490. begin
  491. prefetch(pointer(hp.next)^);
  492. if not(hp.typ in SkipLineInfo) then
  493. begin
  494. current_filepos:=tailineinfo(hp).fileinfo;
  495. { no line info for inlined code }
  496. if do_line and (inlinelevel=0) and not DoNotSplitLine then
  497. WriteSourceLine(hp as tailineinfo);
  498. end;
  499. DoNotSplitLine:=false;
  500. case hp.typ of
  501. ait_comment :
  502. Begin
  503. writer.AsmWrite(asminfo^.comment);
  504. writer.AsmWritePChar(tai_comment(hp).str);
  505. writer.AsmLn;
  506. End;
  507. ait_regalloc :
  508. begin
  509. if (cs_asm_regalloc in current_settings.globalswitches) then
  510. writer.AsmWriteLn(asminfo^.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  511. regallocstr[tai_regalloc(hp).ratype]);
  512. end;
  513. ait_tempalloc :
  514. begin
  515. if (cs_asm_tempalloc in current_settings.globalswitches) then
  516. WriteTempalloc(tai_tempalloc(hp));
  517. end;
  518. ait_section :
  519. begin
  520. if tai_section(hp).sectype<>sec_none then
  521. begin
  522. if asminfo^.id=as_x86_64_masm then
  523. begin
  524. if LasTSecType<>sec_none then
  525. writer.AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  526. writer.AsmLn;
  527. writer.AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  528. end
  529. else
  530. begin
  531. if LasTSecType<>sec_none then
  532. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  533. writer.AsmLn;
  534. if (asminfo^.id=as_i386_wasm) then
  535. s:='DWORD'
  536. else
  537. s:=alignstr(tai_section(hp).secalign);
  538. writer.AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  539. 'SEGMENT'#9+s+' PUBLIC USE32 '''+
  540. secnames[tai_section(hp).sectype]+'''');
  541. end;
  542. end;
  543. LasTSecType:=tai_section(hp).sectype;
  544. end;
  545. ait_align :
  546. begin
  547. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  548. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  549. { HERE UNDER TASM! }
  550. if tai_align_abstract(hp).aligntype>1 then
  551. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  552. end;
  553. ait_datablock :
  554. begin
  555. if tai_datablock(hp).is_global then
  556. writer.AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  557. writer.AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  558. end;
  559. ait_const:
  560. begin
  561. consttype:=tai_const(hp).consttype;
  562. case consttype of
  563. aitconst_uleb128bit,
  564. aitconst_sleb128bit,
  565. aitconst_128bit,
  566. aitconst_64bit,
  567. aitconst_32bit,
  568. aitconst_16bit,
  569. aitconst_8bit,
  570. aitconst_rva_symbol,
  571. aitconst_secrel32_symbol :
  572. begin
  573. writer.AsmWrite(ait_const2str[consttype]);
  574. l:=0;
  575. repeat
  576. if assigned(tai_const(hp).sym) then
  577. begin
  578. if assigned(tai_const(hp).endsym) then
  579. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  580. else
  581. s:=tai_const(hp).sym.name;
  582. if tai_const(hp).value<>0 then
  583. s:=s+tostr_with_plus(tai_const(hp).value);
  584. end
  585. else
  586. s:=tostr(tai_const(hp).value);
  587. writer.AsmWrite(s);
  588. inc(l,length(s));
  589. if (l>line_length) or
  590. (hp.next=nil) or
  591. (tai(hp.next).typ<>ait_const) or
  592. (tai_const(hp.next).consttype<>consttype) then
  593. break;
  594. hp:=tai(hp.next);
  595. writer.AsmWrite(',');
  596. until false;
  597. { Substract section start for secrel32 type }
  598. if consttype=aitconst_secrel32_symbol then
  599. writer.AsmWrite(' - $$');
  600. writer.AsmLn;
  601. end;
  602. else
  603. internalerror(200704253);
  604. end;
  605. end;
  606. ait_realconst:
  607. begin
  608. case tai_realconst(hp).realtyp of
  609. aitrealconst_s32bit:
  610. writer.AsmWriteLn(#9#9'DD'#9+single2str(tai_realconst(hp).value.s32val));
  611. aitrealconst_s64bit:
  612. writer.AsmWriteLn(#9#9'DQ'#9+double2str(tai_realconst(hp).value.s64val));
  613. aitrealconst_s80bit:
  614. writer.AsmWriteLn(#9#9'DT'#9+extended2str(tai_realconst(hp).value.s80val));
  615. aitrealconst_s64comp:
  616. writer.AsmWriteLn(#9#9'DQ'#9+extended2str(tai_realconst(hp).value.s64compval));
  617. else
  618. internalerror(2014050604);
  619. end;
  620. end;
  621. ait_string :
  622. begin
  623. counter := 0;
  624. lines := tai_string(hp).len div line_length;
  625. { separate lines in different parts }
  626. if tai_string(hp).len > 0 then
  627. Begin
  628. for j := 0 to lines-1 do
  629. begin
  630. writer.AsmWrite(#9#9'DB'#9);
  631. quoted:=false;
  632. for i:=counter to counter+line_length-1 do
  633. begin
  634. { it is an ascii character. }
  635. if (ord(tai_string(hp).str[i])>31) and
  636. (ord(tai_string(hp).str[i])<128) and
  637. (tai_string(hp).str[i]<>'"') then
  638. begin
  639. if not(quoted) then
  640. begin
  641. if i>counter then
  642. writer.AsmWrite(',');
  643. writer.AsmWrite('"');
  644. end;
  645. writer.AsmWrite(tai_string(hp).str[i]);
  646. quoted:=true;
  647. end { if > 31 and < 128 and ord('"') }
  648. else
  649. begin
  650. if quoted then
  651. writer.AsmWrite('"');
  652. if i>counter then
  653. writer.AsmWrite(',');
  654. quoted:=false;
  655. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  656. end;
  657. end; { end for i:=0 to... }
  658. if quoted then writer.AsmWrite('"');
  659. writer.AsmWrite(target_info.newline);
  660. counter := counter+line_length;
  661. end; { end for j:=0 ... }
  662. { do last line of lines }
  663. if counter<tai_string(hp).len then
  664. writer.AsmWrite(#9#9'DB'#9);
  665. quoted:=false;
  666. for i:=counter to tai_string(hp).len-1 do
  667. begin
  668. { it is an ascii character. }
  669. if (ord(tai_string(hp).str[i])>31) and
  670. (ord(tai_string(hp).str[i])<128) and
  671. (tai_string(hp).str[i]<>'"') then
  672. begin
  673. if not(quoted) then
  674. begin
  675. if i>counter then
  676. writer.AsmWrite(',');
  677. writer.AsmWrite('"');
  678. end;
  679. writer.AsmWrite(tai_string(hp).str[i]);
  680. quoted:=true;
  681. end { if > 31 and < 128 and " }
  682. else
  683. begin
  684. if quoted then
  685. writer.AsmWrite('"');
  686. if i>counter then
  687. writer.AsmWrite(',');
  688. quoted:=false;
  689. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  690. end;
  691. end; { end for i:=0 to... }
  692. if quoted then
  693. writer.AsmWrite('"');
  694. end;
  695. writer.AsmLn;
  696. end;
  697. ait_label :
  698. begin
  699. if tai_label(hp).labsym.is_used then
  700. begin
  701. writer.AsmWrite(tai_label(hp).labsym.name);
  702. if assigned(hp.next) and not(tai(hp.next).typ in
  703. [ait_const,ait_realconst,ait_string]) then
  704. writer.AsmWriteLn(':')
  705. else
  706. DoNotSplitLine:=true;
  707. end;
  708. end;
  709. ait_symbol :
  710. begin
  711. if tai_symbol(hp).has_value then
  712. internalerror(2009090802);
  713. { wasm is case insensitive, we nned to use only uppercase version
  714. if both a lowercase and an uppercase version are provided }
  715. if (asminfo^.id = as_i386_wasm) then
  716. begin
  717. nhp:=tai(hp.next);
  718. while assigned(nhp) and (nhp.typ in [ait_function_name,ait_force_line]) do
  719. nhp:=tai(nhp.next);
  720. if assigned(nhp) and (tai(nhp).typ=ait_symbol) and
  721. (lower(tai_symbol(nhp).sym.name)=tai_symbol(hp).sym.name) then
  722. begin
  723. writer.AsmWriteln(asminfo^.comment+' '+tai_symbol(hp).sym.name+' removed');
  724. hp:=tai(nhp);
  725. end;
  726. end;
  727. if tai_symbol(hp).is_global then
  728. writer.AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  729. writer.AsmWrite(tai_symbol(hp).sym.name);
  730. if assigned(hp.next) and not(tai(hp.next).typ in
  731. [ait_const,ait_realconst,ait_string]) then
  732. writer.AsmWriteLn(':');
  733. end;
  734. ait_symbol_end :
  735. begin
  736. end;
  737. ait_instruction :
  738. begin
  739. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  740. taicpu(hp).SetOperandOrder(op_intel);
  741. { Reset }
  742. suffix:='';
  743. prefix:= '';
  744. { We need to explicitely set
  745. word prefix to get selectors
  746. to be pushed in 2 bytes PM }
  747. if (taicpu(hp).opsize=S_W) and
  748. (
  749. (
  750. (fixed_opcode=A_PUSH) or
  751. (fixed_opcode=A_POP)
  752. ) and
  753. (taicpu(hp).oper[0]^.typ=top_reg) and
  754. is_segment_reg(taicpu(hp).oper[0]^.reg)
  755. ) then
  756. writer.AsmWriteln(#9#9'DB'#9'066h');
  757. { added prefix instructions, must be on same line as opcode }
  758. if (taicpu(hp).ops = 0) and
  759. ((fixed_opcode = A_REP) or
  760. (fixed_opcode = A_LOCK) or
  761. (fixed_opcode = A_REPE) or
  762. (fixed_opcode = A_REPNZ) or
  763. (fixed_opcode = A_REPZ) or
  764. (fixed_opcode = A_REPNE)) then
  765. Begin
  766. prefix:=std_op2str[fixed_opcode]+#9;
  767. { there can be a stab inbetween when the opcode was on
  768. a different line in the source code }
  769. repeat
  770. hp:=tai(hp.next);
  771. until (hp=nil) or (hp.typ=ait_instruction);
  772. { next instruction ... }
  773. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  774. taicpu(hp).SetOperandOrder(op_intel);
  775. { this is theorically impossible... }
  776. if hp=nil then
  777. begin
  778. writer.AsmWriteLn(#9#9+prefix);
  779. break;
  780. end;
  781. { nasm prefers prefix on a line alone
  782. writer.AsmWriteln(#9#9+prefix); but not masm PM
  783. prefix:=''; }
  784. if asminfo^.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  785. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  786. begin
  787. writer.AsmWriteln(prefix);
  788. prefix:='';
  789. end;
  790. end
  791. else
  792. prefix:= '';
  793. if (asminfo^.id = as_i386_wasm) and
  794. (taicpu(hp).opsize=S_W) and
  795. (fixed_opcode=A_PUSH) and
  796. (taicpu(hp).oper[0]^.typ=top_const) then
  797. begin
  798. writer.AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  799. writer.AsmWrite(#9#9'DW');
  800. end
  801. else if (asminfo^.id=as_x86_64_masm) and
  802. (fixed_opcode=A_MOVQ) then
  803. writer.AsmWrite(#9#9'mov')
  804. else
  805. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]+suffix);
  806. if taicpu(hp).ops<>0 then
  807. begin
  808. if is_calljmp(fixed_opcode) then
  809. begin
  810. writer.AsmWrite(#9);
  811. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  812. end
  813. else
  814. begin
  815. for i:=0to taicpu(hp).ops-1 do
  816. begin
  817. if i=0 then
  818. writer.AsmWrite(#9)
  819. else
  820. writer.AsmWrite(',');
  821. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,(i=2));
  822. end;
  823. end;
  824. end;
  825. writer.AsmLn;
  826. end;
  827. ait_stab,
  828. ait_force_line,
  829. ait_function_name : ;
  830. ait_cutobject :
  831. begin
  832. { only reset buffer if nothing has changed }
  833. if not writer.ClearIfEmpty then
  834. begin
  835. if LasTSecType<>sec_none then
  836. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  837. writer.AsmLn;
  838. writer.AsmWriteLn(#9'END');
  839. writer.AsmClose;
  840. DoAssemble;
  841. writer.AsmCreate(tai_cutobject(hp).place);
  842. end;
  843. { avoid empty files }
  844. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  845. begin
  846. if tai(hp.next).typ=ait_section then
  847. lasTSecType:=tai_section(hp.next).sectype;
  848. hp:=tai(hp.next);
  849. end;
  850. if (asminfo^.id = as_i386_wasm) then
  851. begin
  852. writer.AsmWriteLn(#9'.686p');
  853. writer.AsmWriteLn(#9'.xmm');
  854. end
  855. else
  856. writer.AsmWriteLn(#9'.386p');
  857. {$ifdef i8086}
  858. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  859. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  860. {$endif i8086}
  861. { I was told that this isn't necesarry because }
  862. { the labels generated by FPC are unique (FK) }
  863. { writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix); }
  864. { TODO: PARA is incorrect, must use actual section align }
  865. if lasTSectype<>sec_none then
  866. writer.AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  867. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  868. secnames[lasTSectype]+'''');
  869. writer.MarkEmpty;
  870. end;
  871. ait_marker :
  872. begin
  873. if tai_marker(hp).kind=mark_NoLineInfoStart then
  874. inc(InlineLevel)
  875. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  876. dec(InlineLevel);
  877. end;
  878. ait_directive :
  879. begin
  880. case tai_directive(hp).directive of
  881. asd_nasm_import :
  882. begin
  883. writer.AsmWrite('import ');
  884. writer.AsmWrite(tai_directive(hp).name);
  885. writer.AsmLn;
  886. end;
  887. asd_extern :
  888. begin
  889. writer.AsmWrite('EXTRN ');
  890. writer.AsmWrite(tai_directive(hp).name);
  891. writer.AsmLn;
  892. end;
  893. asd_cpu :
  894. begin
  895. if (asminfo^.id = as_i386_wasm) then
  896. begin
  897. {writer.AsmWrite('.');}
  898. for cpu:=low(tcputype) to high(tcputype) do
  899. begin
  900. if tai_directive(hp).name=CPUTypeStr[CPU] then
  901. begin
  902. { writer.AsmWriteLn(wasm_cpu_name[cpu]); }
  903. break;
  904. end;
  905. end;
  906. end
  907. else
  908. begin
  909. { TODO: implement this properly for TASM/MASM/WASM (.686p, etc.) }
  910. writer.AsmWrite(asminfo^.comment+' CPU ');
  911. writer.AsmWrite(tai_directive(hp).name);
  912. writer.AsmLn;
  913. end;
  914. end
  915. else
  916. internalerror(200509192);
  917. end;
  918. end;
  919. ait_seh_directive :
  920. { Ignore for now };
  921. else
  922. internalerror(10000);
  923. end;
  924. hp:=tai(hp.next);
  925. end;
  926. end;
  927. procedure tx86intelassembler.WriteExternals;
  928. var
  929. sym : TAsmSymbol;
  930. i : longint;
  931. begin
  932. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  933. begin
  934. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  935. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  936. begin
  937. case asminfo^.id of
  938. as_i386_masm,
  939. as_i386_wasm :
  940. writer.AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  941. as_x86_64_masm :
  942. writer.AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  943. else
  944. writer.AsmWriteln(#9'EXTRN'#9+sym.name);
  945. end;
  946. end;
  947. end;
  948. end;
  949. function tx86intelassembler.DoAssemble : boolean;
  950. var
  951. masmobjfn : string;
  952. begin
  953. DoAssemble:=Inherited DoAssemble;
  954. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  955. if (asminfo^.id in [as_i386_masm,as_i386_wasm]) then
  956. begin
  957. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  958. if not(cs_asm_extern in current_settings.globalswitches) then
  959. begin
  960. if Not FileExists(objfilename) and
  961. FileExists(masmobjfn) then
  962. RenameFile(masmobjfn,objfilename);
  963. end
  964. else
  965. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  966. end;
  967. end;
  968. procedure tx86IntelAssembler.WriteAsmList;
  969. var
  970. hal : tasmlisttype;
  971. begin
  972. {$ifdef EXTDEBUG}
  973. if current_module.mainsource<>'' then
  974. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource);
  975. {$endif}
  976. if asminfo^.id<>as_x86_64_masm then
  977. begin
  978. if (asminfo^.id = as_i386_wasm) then
  979. begin
  980. writer.AsmWriteLn(#9'.686p');
  981. writer.AsmWriteLn(#9'.xmm');
  982. end
  983. else
  984. writer.AsmWriteLn(#9'.386p');
  985. { masm 6.11 does not seem to like LOCALS PM }
  986. if (asminfo^.id = as_i386_tasm) then
  987. begin
  988. writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix);
  989. end;
  990. {$ifdef i8086}
  991. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  992. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  993. {$endif i8086}
  994. writer.AsmLn;
  995. end;
  996. WriteExternals;
  997. for hal:=low(TasmlistType) to high(TasmlistType) do
  998. begin
  999. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1000. writetree(current_asmdata.asmlists[hal]);
  1001. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1002. end;
  1003. { better do this at end of WriteTree, but then there comes a trouble with
  1004. al_const which does not have leading ait_section and thus goes out of segment }
  1005. if LastSecType <> sec_none then
  1006. begin
  1007. if asminfo^.id=as_x86_64_masm then
  1008. writer.AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS')
  1009. else
  1010. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  1011. end;
  1012. LastSecType := sec_none;
  1013. writer.AsmWriteLn(#9'END');
  1014. writer.AsmLn;
  1015. {$ifdef EXTDEBUG}
  1016. if current_module.mainsource<>'' then
  1017. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource);
  1018. {$endif EXTDEBUG}
  1019. end;
  1020. {*****************************************************************************
  1021. Initialize
  1022. *****************************************************************************}
  1023. const
  1024. {$ifdef i386}
  1025. as_i386_tasm_info : tasminfo =
  1026. (
  1027. id : as_i386_tasm;
  1028. idtxt : 'TASM';
  1029. asmbin : 'tasm';
  1030. asmcmd : '/m2 /ml $EXTRAOPT $ASM $OBJ';
  1031. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1032. flags : [af_needar,af_labelprefix_only_inside_procedure];
  1033. labelprefix : '@@';
  1034. comment : '; ';
  1035. dollarsign: '$';
  1036. );
  1037. as_i386_masm_info : tasminfo =
  1038. (
  1039. id : as_i386_masm;
  1040. idtxt : 'MASM';
  1041. asmbin : 'masm';
  1042. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1043. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1044. flags : [af_needar];
  1045. labelprefix : '@@';
  1046. comment : '; ';
  1047. dollarsign: '$';
  1048. );
  1049. as_i386_wasm_info : tasminfo =
  1050. (
  1051. id : as_i386_wasm;
  1052. idtxt : 'WASM';
  1053. asmbin : 'wasm';
  1054. asmcmd : '$ASM $EXTRAOPT -6s -fp6 -ms -zq -Fo=$OBJ';
  1055. supported_targets : [system_i386_watcom];
  1056. flags : [af_needar];
  1057. labelprefix : '@@';
  1058. comment : '; ';
  1059. dollarsign: '$';
  1060. );
  1061. {$endif i386}
  1062. {$ifdef x86_64}
  1063. as_x86_64_masm_info : tasminfo =
  1064. (
  1065. id : as_x86_64_masm;
  1066. idtxt : 'MASM';
  1067. asmbin : 'ml64';
  1068. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1069. supported_targets : [system_x86_64_win64];
  1070. flags : [af_needar];
  1071. labelprefix : '@@';
  1072. comment : '; ';
  1073. dollarsign: '$';
  1074. );
  1075. {$endif x86_64}
  1076. initialization
  1077. {$ifdef x86_64}
  1078. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  1079. {$endif x86_64}
  1080. {$ifdef i386}
  1081. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  1082. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  1083. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  1084. {$endif i386}
  1085. end.