agx86int.pas 42 KB

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