agx86int.pas 42 KB

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