agx86int.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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(ApplyAsmSymbolRestrictions(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(ApplyAsmSymbolRestrictions(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(ApplyAsmSymbolRestrictions(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_section :
  514. begin
  515. ResetSourceLines;
  516. if tai_section(hp).sectype<>sec_none then
  517. begin
  518. if asminfo^.id=as_x86_64_masm then
  519. begin
  520. if LasTSecType<>sec_none then
  521. writer.AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  522. writer.AsmLn;
  523. writer.AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  524. end
  525. else
  526. begin
  527. if LasTSecType<>sec_none then
  528. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  529. writer.AsmLn;
  530. if (asminfo^.id=as_i386_wasm) then
  531. s:='DWORD'
  532. else
  533. s:=alignstr(tai_section(hp).secalign);
  534. writer.AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  535. 'SEGMENT'#9+s+' PUBLIC USE32 '''+
  536. secnames[tai_section(hp).sectype]+'''');
  537. end;
  538. end;
  539. LasTSecType:=tai_section(hp).sectype;
  540. end;
  541. ait_align :
  542. begin
  543. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  544. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  545. { HERE UNDER TASM! }
  546. if tai_align_abstract(hp).aligntype>1 then
  547. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  548. end;
  549. ait_datablock :
  550. begin
  551. if tai_datablock(hp).is_global then
  552. writer.AsmWriteLn(#9'PUBLIC'#9+ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  553. writer.AsmWriteLn(PadTabs(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name),#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  554. end;
  555. ait_const:
  556. begin
  557. consttype:=tai_const(hp).consttype;
  558. case consttype of
  559. aitconst_uleb128bit,
  560. aitconst_sleb128bit,
  561. aitconst_128bit,
  562. aitconst_64bit,
  563. aitconst_32bit,
  564. aitconst_16bit,
  565. aitconst_8bit,
  566. aitconst_16bit_unaligned,
  567. aitconst_32bit_unaligned,
  568. aitconst_64bit_unaligned,
  569. aitconst_rva_symbol,
  570. aitconst_secrel32_symbol :
  571. begin
  572. writer.AsmWrite(ait_const2str[consttype]);
  573. l:=0;
  574. tokens:=1;
  575. repeat
  576. if assigned(tai_const(hp).sym) then
  577. begin
  578. if assigned(tai_const(hp).endsym) then
  579. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  580. else
  581. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  582. if tai_const(hp).value<>0 then
  583. s:=s+tostr_with_plus(tai_const(hp).value);
  584. end
  585. else
  586. s:=tostr(tai_const(hp).value);
  587. writer.AsmWrite(s);
  588. inc(l,length(s));
  589. inc(tokens);
  590. if (l>line_length) or
  591. (tokens>max_tokens) or
  592. (hp.next=nil) or
  593. (tai(hp.next).typ<>ait_const) or
  594. (tai_const(hp.next).consttype<>consttype) then
  595. break;
  596. hp:=tai(hp.next);
  597. writer.AsmWrite(',');
  598. until false;
  599. { Substract section start for secrel32 type }
  600. if consttype=aitconst_secrel32_symbol then
  601. writer.AsmWrite(' - $$');
  602. writer.AsmLn;
  603. end;
  604. else
  605. internalerror(200704253);
  606. end;
  607. end;
  608. ait_realconst:
  609. begin
  610. case tai_realconst(hp).realtyp of
  611. aitrealconst_s32bit:
  612. begin
  613. if (asminfo^.id = as_i386_wasm) and (IsInfinite(tai_realconst(hp).value.s32val)) then
  614. begin
  615. { Watcom Wasm does not handle Infinity }
  616. if Sign(tai_realconst(hp).value.s32val)=PositiveValue then
  617. writer.AsmWriteln(#9#9'DB'#9'0,0,80h,7Fh')
  618. else
  619. writer.AsmWriteln(#9#9'DW'#9'0,0,80h,FFh');
  620. end
  621. else if (asminfo^.id = as_i386_wasm) and (IsNan(tai_realconst(hp).value.s32val)) then
  622. writer.AsmWriteln(#9#9'DB'#9'1,0,80h,7Fh')
  623. else
  624. writer.AsmWriteLn(#9#9'DD'#9+single2str(tai_realconst(hp).value.s32val));
  625. end;
  626. aitrealconst_s64bit:
  627. begin
  628. if (asminfo^.id = as_i386_wasm) and (IsInfinite(tai_realconst(hp).value.s64val)) then
  629. begin
  630. { Watcom Wasm does not handle Infinity }
  631. if Sign(tai_realconst(hp).value.s64val)=PositiveValue then
  632. writer.AsmWriteln(#9#9'DW'#9'0,0,0,7FF0h')
  633. else
  634. writer.AsmWriteln(#9#9'DW'#9'0,0,0,FFF0h');
  635. end
  636. else if (asminfo^.id = as_i386_wasm) and (IsNan(tai_realconst(hp).value.s64val)) then
  637. writer.AsmWriteln(#9#9'DW'#9'0,0,0,0,7FF8h')
  638. else
  639. writer.AsmWriteLn(#9#9'DQ'#9+double2str(tai_realconst(hp).value.s64val));
  640. end;
  641. aitrealconst_s80bit:
  642. if (asminfo^.id = as_i386_wasm) and (IsInfinite(tai_realconst(hp).value.s80val)) then
  643. begin
  644. { Watcom Wasm does not handle Infinity }
  645. if Sign(tai_realconst(hp).value.s80val)=PositiveValue then
  646. writer.AsmWriteln(#9#9'DW'#9'0,0,0,8000h,7FFFh')
  647. else
  648. writer.AsmWriteln(#9#9'DW'#9'0,0,0,8000h,FFFFh');
  649. end
  650. else if (asminfo^.id = as_i386_wasm) and (IsNan(tai_realconst(hp).value.s80val)) then
  651. writer.AsmWriteln(#9#9'DW'#9'0,0,0,0xC000,0x7FFF')
  652. else
  653. writer.AsmWriteLn(#9#9'DT'#9+extended2str(tai_realconst(hp).value.s80val));
  654. aitrealconst_s64comp:
  655. writer.AsmWriteLn(#9#9'DQ'#9+extended2str(tai_realconst(hp).value.s64compval));
  656. else
  657. internalerror(2014050606);
  658. end;
  659. end;
  660. ait_string :
  661. begin
  662. counter := 0;
  663. lines := tai_string(hp).len div line_length;
  664. { separate lines in different parts }
  665. if tai_string(hp).len > 0 then
  666. Begin
  667. for j := 0 to lines-1 do
  668. begin
  669. writer.AsmWrite(#9#9'DB'#9);
  670. quoted:=false;
  671. for i:=counter to counter+line_length-1 do
  672. begin
  673. { it is an ascii character. }
  674. if (ord(tai_string(hp).str[i])>31) and
  675. (ord(tai_string(hp).str[i])<128) and
  676. (tai_string(hp).str[i]<>'"') then
  677. begin
  678. if not(quoted) then
  679. begin
  680. if i>counter then
  681. writer.AsmWrite(',');
  682. writer.AsmWrite('"');
  683. end;
  684. writer.AsmWrite(tai_string(hp).str[i]);
  685. quoted:=true;
  686. end { if > 31 and < 128 and ord('"') }
  687. else
  688. begin
  689. if quoted then
  690. writer.AsmWrite('"');
  691. if i>counter then
  692. writer.AsmWrite(',');
  693. quoted:=false;
  694. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  695. end;
  696. end; { end for i:=0 to... }
  697. if quoted then writer.AsmWrite('"');
  698. writer.AsmWrite(target_info.newline);
  699. counter := counter+line_length;
  700. end; { end for j:=0 ... }
  701. { do last line of lines }
  702. if counter<tai_string(hp).len then
  703. writer.AsmWrite(#9#9'DB'#9);
  704. quoted:=false;
  705. for i:=counter to tai_string(hp).len-1 do
  706. begin
  707. { it is an ascii character. }
  708. if (ord(tai_string(hp).str[i])>31) and
  709. (ord(tai_string(hp).str[i])<128) and
  710. (tai_string(hp).str[i]<>'"') then
  711. begin
  712. if not(quoted) then
  713. begin
  714. if i>counter then
  715. writer.AsmWrite(',');
  716. writer.AsmWrite('"');
  717. end;
  718. writer.AsmWrite(tai_string(hp).str[i]);
  719. quoted:=true;
  720. end { if > 31 and < 128 and " }
  721. else
  722. begin
  723. if quoted then
  724. writer.AsmWrite('"');
  725. if i>counter then
  726. writer.AsmWrite(',');
  727. quoted:=false;
  728. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  729. end;
  730. end; { end for i:=0 to... }
  731. if quoted then
  732. writer.AsmWrite('"');
  733. end;
  734. writer.AsmLn;
  735. end;
  736. ait_label :
  737. begin
  738. if tai_label(hp).labsym.is_used then
  739. begin
  740. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name));
  741. if not assigned(hp.next) or not(tai(hp.next).typ in
  742. [ait_const,ait_realconst,ait_string]) then
  743. writer.AsmWriteLn(':')
  744. else
  745. DoNotSplitLine:=true;
  746. end;
  747. end;
  748. ait_symbol :
  749. begin
  750. if tai_symbol(hp).has_value then
  751. internalerror(2009090802);
  752. { wasm is case insensitive, we need to use only uppercase version
  753. if both a lowercase and an uppercase version are provided }
  754. if (asminfo^.id = as_i386_wasm) then
  755. begin
  756. nhp:=tai(hp.next);
  757. while assigned(nhp) and (nhp.typ in [ait_function_name,ait_force_line]) do
  758. nhp:=tai(nhp.next);
  759. if assigned(nhp) and (tai(nhp).typ=ait_symbol) and
  760. (lower(tai_symbol(nhp).sym.name)=tai_symbol(hp).sym.name) then
  761. begin
  762. writer.AsmWriteln(asminfo^.comment+' '+tai_symbol(hp).sym.name+' removed');
  763. hp:=tai(nhp);
  764. end;
  765. end;
  766. if tai_symbol(hp).is_global then
  767. writer.AsmWriteLn(#9'PUBLIC'#9+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  768. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  769. if not assigned(hp.next) or not(tai(hp.next).typ in
  770. [ait_const,ait_realconst,ait_string]) then
  771. writer.AsmWriteLn(':');
  772. end;
  773. ait_symbol_end :
  774. begin
  775. end;
  776. ait_instruction :
  777. begin
  778. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  779. taicpu(hp).SetOperandOrder(op_intel);
  780. { Reset }
  781. suffix:='';
  782. prefix:= '';
  783. { We need to explicitely set
  784. word prefix to get selectors
  785. to be pushed in 2 bytes PM }
  786. if (taicpu(hp).opsize=S_W) and
  787. (
  788. (
  789. (fixed_opcode=A_PUSH) or
  790. (fixed_opcode=A_POP)
  791. ) and
  792. (taicpu(hp).oper[0]^.typ=top_reg) and
  793. is_segment_reg(taicpu(hp).oper[0]^.reg)
  794. ) then
  795. writer.AsmWriteln(#9#9'DB'#9'066h');
  796. { added prefix instructions, must be on same line as opcode }
  797. if (taicpu(hp).ops = 0) and
  798. ((fixed_opcode = A_REP) or
  799. (fixed_opcode = A_LOCK) or
  800. (fixed_opcode = A_REPE) or
  801. (fixed_opcode = A_REPNZ) or
  802. (fixed_opcode = A_REPZ) or
  803. (fixed_opcode = A_REPNE)) then
  804. Begin
  805. prefix:=std_op2str[fixed_opcode]+#9;
  806. { there can be a stab inbetween when the opcode was on
  807. a different line in the source code }
  808. repeat
  809. hp:=tai(hp.next);
  810. until (hp=nil) or (hp.typ=ait_instruction);
  811. { next instruction ... }
  812. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  813. taicpu(hp).SetOperandOrder(op_intel);
  814. { this is theorically impossible... }
  815. if hp=nil then
  816. begin
  817. writer.AsmWriteLn(#9#9+prefix);
  818. break;
  819. end;
  820. { nasm prefers prefix on a line alone
  821. writer.AsmWriteln(#9#9+prefix); but not masm PM
  822. prefix:=''; }
  823. if asminfo^.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  824. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  825. begin
  826. writer.AsmWriteln(prefix);
  827. prefix:='';
  828. end;
  829. end
  830. else
  831. prefix:= '';
  832. if (asminfo^.id = as_i386_wasm) and
  833. (taicpu(hp).opsize=S_W) and
  834. (fixed_opcode=A_PUSH) and
  835. (taicpu(hp).oper[0]^.typ=top_const) then
  836. begin
  837. writer.AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  838. writer.AsmWrite(#9#9'DW');
  839. end
  840. else if (asminfo^.id=as_x86_64_masm) and
  841. (fixed_opcode=A_MOVQ) then
  842. writer.AsmWrite(#9#9'mov')
  843. {$ifdef I386}
  844. else if (asminfo^.id = as_i386_wasm) and ((fixed_opcode=A_RETD)
  845. or (fixed_opcode=A_RETND) or (fixed_opcode=A_RETFD)) then
  846. begin
  847. { no 'd' suffix for Watcom assembler }
  848. case fixed_opcode of
  849. A_RETD:
  850. writer.AsmWrite(#9#9'ret');
  851. A_RETND:
  852. writer.AsmWrite(#9#9'retn');
  853. A_RETFD:
  854. writer.AsmWrite(#9#9'retf');
  855. else
  856. internalerror(2019050907);
  857. end
  858. end
  859. {$endif I386}
  860. else
  861. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]+suffix);
  862. if taicpu(hp).ops<>0 then
  863. begin
  864. if is_calljmp(fixed_opcode) then
  865. begin
  866. writer.AsmWrite(#9);
  867. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  868. end
  869. else
  870. begin
  871. for i:=0to taicpu(hp).ops-1 do
  872. begin
  873. if i=0 then
  874. writer.AsmWrite(#9)
  875. else
  876. writer.AsmWrite(',');
  877. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,(i=2));
  878. end;
  879. end;
  880. end;
  881. writer.AsmLn;
  882. end;
  883. ait_stab,
  884. ait_force_line,
  885. ait_function_name : ;
  886. ait_cutobject :
  887. begin
  888. { only reset buffer if nothing has changed }
  889. if not writer.ClearIfEmpty then
  890. begin
  891. if LasTSecType<>sec_none then
  892. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  893. writer.AsmLn;
  894. writer.AsmWriteLn(#9'END');
  895. writer.AsmClose;
  896. DoAssemble;
  897. writer.AsmCreate(tai_cutobject(hp).place);
  898. end;
  899. { avoid empty files }
  900. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  901. begin
  902. if tai(hp.next).typ=ait_section then
  903. lasTSecType:=tai_section(hp.next).sectype;
  904. hp:=tai(hp.next);
  905. end;
  906. if (asminfo^.id = as_i386_wasm) then
  907. begin
  908. writer.AsmWriteLn(#9'.686p');
  909. writer.AsmWriteLn(#9'.xmm');
  910. end
  911. else
  912. writer.AsmWriteLn(#9'.386p');
  913. {$ifdef i8086}
  914. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  915. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  916. {$endif i8086}
  917. { I was told that this isn't necesarry because }
  918. { the labels generated by FPC are unique (FK) }
  919. { writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix); }
  920. { TODO: PARA is incorrect, must use actual section align }
  921. if lasTSectype<>sec_none then
  922. writer.AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  923. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  924. secnames[lasTSectype]+'''');
  925. writer.MarkEmpty;
  926. end;
  927. ait_marker :
  928. begin
  929. if tai_marker(hp).kind=mark_NoLineInfoStart then
  930. inc(InlineLevel)
  931. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  932. dec(InlineLevel);
  933. end;
  934. ait_directive :
  935. begin
  936. case tai_directive(hp).directive of
  937. asd_nasm_import :
  938. begin
  939. writer.AsmWrite('import ');
  940. writer.AsmWrite(tai_directive(hp).name);
  941. writer.AsmLn;
  942. end;
  943. asd_extern :
  944. begin
  945. writer.AsmWrite('EXTRN ');
  946. writer.AsmWrite(tai_directive(hp).name);
  947. writer.AsmLn;
  948. end;
  949. asd_cpu :
  950. begin
  951. if (asminfo^.id = as_i386_wasm) then
  952. begin
  953. {writer.AsmWrite('.');}
  954. for cpu:=low(tcputype) to high(tcputype) do
  955. begin
  956. if tai_directive(hp).name=CPUTypeStr[CPU] then
  957. begin
  958. { writer.AsmWriteLn(wasm_cpu_name[cpu]); }
  959. break;
  960. end;
  961. end;
  962. end
  963. else
  964. begin
  965. { TODO: implement this properly for TASM/MASM/WASM (.686p, etc.) }
  966. writer.AsmWrite(asminfo^.comment+' CPU ');
  967. writer.AsmWrite(tai_directive(hp).name);
  968. writer.AsmLn;
  969. end;
  970. end
  971. else
  972. internalerror(200509192);
  973. end;
  974. end;
  975. ait_seh_directive :
  976. { Ignore for now };
  977. else
  978. if not WriteComments(hp) then
  979. internalerror(2020100802);
  980. end;
  981. hp:=tai(hp.next);
  982. end;
  983. end;
  984. procedure tx86intelassembler.WriteExternals;
  985. var
  986. sym : TAsmSymbol;
  987. i : longint;
  988. begin
  989. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  990. begin
  991. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  992. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  993. begin
  994. case asminfo^.id of
  995. as_i386_masm,
  996. as_i386_wasm :
  997. writer.AsmWriteln(#9'EXTRN'#9+ApplyAsmSymbolRestrictions(sym.name)+': NEAR');
  998. as_x86_64_masm :
  999. writer.AsmWriteln(#9'EXTRN'#9+ApplyAsmSymbolRestrictions(sym.name)+': PROC');
  1000. else
  1001. writer.AsmWriteln(#9'EXTRN'#9+ApplyAsmSymbolRestrictions(sym.name));
  1002. end;
  1003. end;
  1004. end;
  1005. end;
  1006. function tx86intelassembler.DoAssemble : boolean;
  1007. var
  1008. masmobjfn : string;
  1009. begin
  1010. DoAssemble:=Inherited DoAssemble;
  1011. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  1012. if (asminfo^.id in [as_i386_masm,as_i386_wasm]) then
  1013. begin
  1014. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  1015. if not(cs_asm_extern in current_settings.globalswitches) then
  1016. begin
  1017. if Not FileExists(objfilename) and
  1018. FileExists(masmobjfn) then
  1019. RenameFile(masmobjfn,objfilename);
  1020. end
  1021. else
  1022. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  1023. end;
  1024. end;
  1025. procedure tx86IntelAssembler.WriteAsmList;
  1026. var
  1027. hal : tasmlisttype;
  1028. begin
  1029. {$ifdef EXTDEBUG}
  1030. if current_module.mainsource<>'' then
  1031. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource);
  1032. {$endif}
  1033. if asminfo^.id<>as_x86_64_masm then
  1034. begin
  1035. if (asminfo^.id = as_i386_wasm) then
  1036. begin
  1037. writer.AsmWriteLn(#9'.686p');
  1038. writer.AsmWriteLn(#9'.xmm');
  1039. end
  1040. else
  1041. writer.AsmWriteLn(#9'.386p');
  1042. { masm 6.11 does not seem to like LOCALS PM }
  1043. if (asminfo^.id = as_i386_tasm) then
  1044. begin
  1045. writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix);
  1046. end;
  1047. {$ifdef i8086}
  1048. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  1049. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  1050. {$endif i8086}
  1051. writer.AsmLn;
  1052. end;
  1053. WriteExternals;
  1054. for hal:=low(TasmlistType) to high(TasmlistType) do
  1055. begin
  1056. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1057. writetree(current_asmdata.asmlists[hal]);
  1058. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1059. end;
  1060. { better do this at end of WriteTree, but then there comes a trouble with
  1061. al_const which does not have leading ait_section and thus goes out of segment }
  1062. if LastSecType <> sec_none then
  1063. begin
  1064. if asminfo^.id=as_x86_64_masm then
  1065. writer.AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS')
  1066. else
  1067. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  1068. end;
  1069. LastSecType := sec_none;
  1070. writer.AsmWriteLn(#9'END');
  1071. writer.AsmLn;
  1072. {$ifdef EXTDEBUG}
  1073. if current_module.mainsource<>'' then
  1074. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource);
  1075. {$endif EXTDEBUG}
  1076. end;
  1077. {*****************************************************************************
  1078. Initialize
  1079. *****************************************************************************}
  1080. const
  1081. {$ifdef i386}
  1082. as_i386_tasm_info : tasminfo =
  1083. (
  1084. id : as_i386_tasm;
  1085. idtxt : 'TASM';
  1086. asmbin : 'tasm';
  1087. asmcmd : '/m2 /ml $EXTRAOPT $ASM $OBJ';
  1088. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1089. flags : [af_needar,af_labelprefix_only_inside_procedure];
  1090. labelprefix : '@@';
  1091. labelmaxlen : -1;
  1092. comment : '; ';
  1093. dollarsign: '$';
  1094. );
  1095. as_i386_masm_info : tasminfo =
  1096. (
  1097. id : as_i386_masm;
  1098. idtxt : 'MASM';
  1099. asmbin : 'masm';
  1100. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1101. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1102. flags : [af_needar];
  1103. labelprefix : '@@';
  1104. labelmaxlen : -1;
  1105. comment : '; ';
  1106. dollarsign: '$';
  1107. );
  1108. as_i386_wasm_info : tasminfo =
  1109. (
  1110. id : as_i386_wasm;
  1111. idtxt : 'WASM';
  1112. asmbin : 'wasm';
  1113. asmcmd : '$ASM $EXTRAOPT -6s -fp6 -ms -zq -Fo=$OBJ';
  1114. supported_targets : [system_i386_watcom];
  1115. flags : [af_needar];
  1116. labelprefix : '@@';
  1117. labelmaxlen : 247;
  1118. comment : '; ';
  1119. dollarsign: '$';
  1120. );
  1121. {$endif i386}
  1122. {$ifdef x86_64}
  1123. as_x86_64_masm_info : tasminfo =
  1124. (
  1125. id : as_x86_64_masm;
  1126. idtxt : 'MASM';
  1127. asmbin : 'ml64';
  1128. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1129. supported_targets : [system_x86_64_win64];
  1130. flags : [af_needar];
  1131. labelprefix : '@@';
  1132. labelmaxlen : -1;
  1133. comment : '; ';
  1134. dollarsign: '$';
  1135. );
  1136. {$endif x86_64}
  1137. initialization
  1138. {$ifdef x86_64}
  1139. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  1140. {$endif x86_64}
  1141. {$ifdef i386}
  1142. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  1143. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  1144. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  1145. {$endif i386}
  1146. end.