agx86int.pas 42 KB

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