agx86int.pas 41 KB

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