agx86int.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for Intel syntax with Intel i386+
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {
  18. This unit implements an asmoutput class for Intel syntax with Intel i386+
  19. }
  20. unit agx86int;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cpubase,constexp,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  26. type
  27. Tx86IntelAssembler = class(TExternalAssembler)
  28. private
  29. procedure WriteReference(var ref : treference);
  30. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  31. procedure WriteOper_jmp(const o:toper;s : topsize);
  32. public
  33. function single2str(d : single) : string; override;
  34. function double2str(d : double) : string; override;
  35. function extended2str(e : extended) : string; override;
  36. function comp2str(d : bestreal) : string;
  37. procedure WriteTree(p:TAsmList);override;
  38. procedure WriteAsmList;override;
  39. Function DoAssemble:boolean;override;
  40. procedure WriteExternals;
  41. end;
  42. implementation
  43. uses
  44. SysUtils,math,
  45. cutils,globtype,globals,systems,cclasses,
  46. verbose,cscript,cpuinfo,
  47. itx86int,
  48. cgbase
  49. {$ifdef EXTDEBUG}
  50. ,fmodule
  51. {$endif EXTDEBUG}
  52. ;
  53. const
  54. line_length = 70;
  55. max_tokens : longint = 25;
  56. (*
  57. wasm_cpu_name : array[tcputype] of string = (
  58. {$if defined(x86_64)}
  59. 'IA64', // cpu_none,
  60. '686', // cpu_athlon64,
  61. '686', // cpu_core_i,
  62. '686', // cpu_core_avx,
  63. '686' // cpu_core_avx2
  64. {$elseif defined(i386)}
  65. 'IA64', // cpu_none,
  66. '386', // cpu_386,
  67. '486', // cpu_486,
  68. '586', // cpu_Pentium,
  69. '686', // cpu_Pentium2,
  70. '686', // cpu_Pentium3,
  71. '686', // cpu_Pentium4,
  72. '686', // cpu_PentiumM,
  73. '686', // cpu_core_i,
  74. '686', // cpu_core_avx,
  75. '686' // cpu_core_avx2
  76. {$elseif defined(i8086)}
  77. 'IA64', // cpu_none
  78. '8086', // cpu_8086
  79. '186', // cpu_186
  80. '286', // cpu_286
  81. '386', // cpu_386
  82. '486', // cpu_486
  83. '586', // cpu_Pentium
  84. '686', // cpu_Pentium2
  85. '686', // cpu_Pentium3
  86. '686', // cpu_Pentium4
  87. '686' // cpu_PentiumM
  88. {$endif}
  89. );
  90. *)
  91. secnames : array[TAsmSectiontype] of string[4] = ('','',
  92. 'CODE','DATA','DATA','DATA','BSS','TLS',
  93. '','','','','','',
  94. '','','','',
  95. '',
  96. '',
  97. '',
  98. '',
  99. '',
  100. '','','','','','',
  101. '',
  102. '',
  103. '',
  104. '',
  105. '',
  106. '',
  107. '',
  108. '',
  109. '',
  110. '',
  111. '',
  112. '',
  113. '',
  114. '',
  115. '',
  116. '',
  117. '',
  118. '',
  119. '',
  120. '',
  121. '',
  122. '',
  123. '',
  124. '',
  125. '',
  126. '',
  127. '',
  128. '',
  129. '',
  130. '',
  131. '',
  132. '',
  133. '',
  134. '',
  135. '',
  136. '',
  137. '',
  138. '',
  139. '',
  140. '',
  141. ''
  142. );
  143. secnamesml64 : array[TAsmSectiontype] of string[7] = ('','',
  144. '_TEXT','_DATA','_DATA','_DATA','_BSS','_TLS',
  145. '','','','',
  146. 'idata$2','idata$4','idata$5','idata$6','idata$7','edata',
  147. '',
  148. '',
  149. '',
  150. '',
  151. '',
  152. '','','','','','',
  153. '',
  154. '',
  155. '',
  156. '',
  157. '',
  158. '',
  159. '',
  160. '',
  161. '',
  162. '',
  163. '',
  164. '',
  165. '',
  166. '',
  167. '',
  168. '',
  169. '',
  170. '',
  171. '',
  172. '',
  173. '',
  174. '',
  175. '',
  176. '',
  177. '',
  178. '',
  179. '',
  180. '',
  181. '',
  182. '',
  183. '',
  184. '',
  185. '',
  186. '',
  187. '',
  188. '',
  189. '',
  190. '',
  191. '',
  192. '',
  193. ''
  194. );
  195. function TX86IntelAssembler.single2str(d : single) : string;
  196. var
  197. hs : string;
  198. p : byte;
  199. begin
  200. str(d,hs);
  201. { nasm expects a lowercase e }
  202. p:=pos('E',hs);
  203. if p>0 then
  204. hs[p]:='e';
  205. p:=pos('+',hs);
  206. if p>0 then
  207. delete(hs,p,1);
  208. single2str:=lower(hs);
  209. end;
  210. function TX86IntelAssembler.double2str(d : double) : string;
  211. var
  212. hs : string;
  213. p : byte;
  214. begin
  215. str(d,hs);
  216. { nasm expects a lowercase e }
  217. p:=pos('E',hs);
  218. if p>0 then
  219. hs[p]:='e';
  220. p:=pos('+',hs);
  221. if p>0 then
  222. delete(hs,p,1);
  223. double2str:=lower(hs);
  224. end;
  225. function TX86IntelAssembler.extended2str(e : extended) : string;
  226. var
  227. hs : string;
  228. p : byte;
  229. begin
  230. str(e,hs);
  231. { nasm expects a lowercase e }
  232. p:=pos('E',hs);
  233. if p>0 then
  234. hs[p]:='e';
  235. p:=pos('+',hs);
  236. if p>0 then
  237. delete(hs,p,1);
  238. extended2str:=lower(hs);
  239. end;
  240. function TX86IntelAssembler.comp2str(d : bestreal) : string;
  241. type
  242. pdouble = ^double;
  243. var
  244. c : comp;
  245. dd : pdouble;
  246. begin
  247. c:=comp(d);
  248. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  249. comp2str:=double2str(dd^);
  250. end;
  251. { MASM supports aligns up to 8192 }
  252. function alignstr(b : longint) : string;
  253. begin
  254. case b of
  255. 1: result:='BYTE';
  256. 2: result:='WORD';
  257. 4: result:='DWORD';
  258. 0,
  259. 16: result:='PARA';
  260. 256: result:='PAGE';
  261. else
  262. result:='ALIGN('+tostr(b)+')';
  263. end;
  264. end;
  265. {****************************************************************************
  266. tx86IntelAssembler
  267. ****************************************************************************}
  268. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  269. var
  270. first : boolean;
  271. begin
  272. with ref do
  273. begin
  274. first:=true;
  275. if segment<>NR_NO then
  276. writer.AsmWrite(masm_regname(segment)+':[')
  277. else
  278. writer.AsmWrite('[');
  279. if assigned(symbol) then
  280. begin
  281. if (asminfo^.id = as_i386_tasm) then
  282. writer.AsmWrite('dword ptr ');
  283. writer.AsmWrite(symbol.name);
  284. first:=false;
  285. end;
  286. if (base<>NR_NO) then
  287. begin
  288. if not(first) then
  289. writer.AsmWrite('+')
  290. else
  291. first:=false;
  292. {$ifdef x86_64}
  293. { ml64 needs [$+foo] instead of [rip+foo] }
  294. if (base=NR_RIP) and (asminfo^.id=as_x86_64_masm) then
  295. writer.AsmWrite('$')
  296. else
  297. {$endif x86_64}
  298. writer.AsmWrite(masm_regname(base));
  299. end;
  300. if (index<>NR_NO) then
  301. begin
  302. if not(first) then
  303. writer.AsmWrite('+')
  304. else
  305. first:=false;
  306. writer.AsmWrite(masm_regname(index));
  307. if scalefactor<>0 then
  308. writer.AsmWrite('*'+tostr(scalefactor));
  309. end;
  310. if offset<0 then
  311. begin
  312. writer.AsmWrite(tostr(offset));
  313. first:=false;
  314. end
  315. else if (offset>0) then
  316. begin
  317. writer.AsmWrite('+'+tostr(offset));
  318. first:=false;
  319. end;
  320. if first then
  321. writer.AsmWrite('0');
  322. writer.AsmWrite(']');
  323. end;
  324. end;
  325. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  326. begin
  327. case o.typ of
  328. top_reg :
  329. writer.AsmWrite(masm_regname(o.reg));
  330. top_const :
  331. writer.AsmWrite(tostr(o.val));
  332. top_ref :
  333. begin
  334. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  335. begin
  336. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  337. (opcode <> A_LFS)
  338. {$ifndef x86_64}
  339. and (opcode <> A_LDS) and (opcode <> A_LES)
  340. {$endif x86_64}
  341. ) then
  342. Begin
  343. case s of
  344. S_B : writer.AsmWrite('byte ptr ');
  345. S_W : writer.AsmWrite('word ptr ');
  346. S_L : writer.AsmWrite('dword ptr ');
  347. S_Q : writer.AsmWrite('qword ptr ');
  348. S_IS : writer.AsmWrite('word ptr ');
  349. S_IL : writer.AsmWrite('dword ptr ');
  350. S_IQ : writer.AsmWrite('qword ptr ');
  351. S_FS : writer.AsmWrite('dword ptr ');
  352. S_FL : writer.AsmWrite('qword ptr ');
  353. S_T,
  354. S_FX : writer.AsmWrite('tbyte ptr ');
  355. S_BW : if dest then
  356. writer.AsmWrite('word ptr ')
  357. else
  358. writer.AsmWrite('byte ptr ');
  359. S_BL : if dest then
  360. writer.AsmWrite('dword ptr ')
  361. else
  362. writer.AsmWrite('byte ptr ');
  363. S_WL : if dest then
  364. writer.AsmWrite('dword ptr ')
  365. else
  366. writer.AsmWrite('word ptr ');
  367. S_XMM: writer.AsmWrite('xmmword ptr ');
  368. S_YMM: writer.AsmWrite('ymmword ptr ');
  369. {$ifdef x86_64}
  370. S_BQ : if dest then
  371. writer.AsmWrite('qword ptr ')
  372. else
  373. writer.AsmWrite('byte ptr ');
  374. S_WQ : if dest then
  375. writer.AsmWrite('qword ptr ')
  376. else
  377. writer.AsmWrite('word ptr ');
  378. S_LQ : if dest then
  379. writer.AsmWrite('qword ptr ')
  380. else
  381. writer.AsmWrite('dword ptr ');
  382. {$endif x86_64}
  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. end
  867. end
  868. {$endif I386}
  869. else
  870. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]+suffix);
  871. if taicpu(hp).ops<>0 then
  872. begin
  873. if is_calljmp(fixed_opcode) then
  874. begin
  875. writer.AsmWrite(#9);
  876. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  877. end
  878. else
  879. begin
  880. for i:=0to taicpu(hp).ops-1 do
  881. begin
  882. if i=0 then
  883. writer.AsmWrite(#9)
  884. else
  885. writer.AsmWrite(',');
  886. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,(i=2));
  887. end;
  888. end;
  889. end;
  890. writer.AsmLn;
  891. end;
  892. ait_stab,
  893. ait_force_line,
  894. ait_function_name : ;
  895. ait_cutobject :
  896. begin
  897. { only reset buffer if nothing has changed }
  898. if not writer.ClearIfEmpty then
  899. begin
  900. if LasTSecType<>sec_none then
  901. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  902. writer.AsmLn;
  903. writer.AsmWriteLn(#9'END');
  904. writer.AsmClose;
  905. DoAssemble;
  906. writer.AsmCreate(tai_cutobject(hp).place);
  907. end;
  908. { avoid empty files }
  909. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  910. begin
  911. if tai(hp.next).typ=ait_section then
  912. lasTSecType:=tai_section(hp.next).sectype;
  913. hp:=tai(hp.next);
  914. end;
  915. if (asminfo^.id = as_i386_wasm) then
  916. begin
  917. writer.AsmWriteLn(#9'.686p');
  918. writer.AsmWriteLn(#9'.xmm');
  919. end
  920. else
  921. writer.AsmWriteLn(#9'.386p');
  922. {$ifdef i8086}
  923. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  924. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  925. {$endif i8086}
  926. { I was told that this isn't necesarry because }
  927. { the labels generated by FPC are unique (FK) }
  928. { writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix); }
  929. { TODO: PARA is incorrect, must use actual section align }
  930. if lasTSectype<>sec_none then
  931. writer.AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  932. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  933. secnames[lasTSectype]+'''');
  934. writer.MarkEmpty;
  935. end;
  936. ait_marker :
  937. begin
  938. if tai_marker(hp).kind=mark_NoLineInfoStart then
  939. inc(InlineLevel)
  940. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  941. dec(InlineLevel);
  942. end;
  943. ait_directive :
  944. begin
  945. case tai_directive(hp).directive of
  946. asd_nasm_import :
  947. begin
  948. writer.AsmWrite('import ');
  949. writer.AsmWrite(tai_directive(hp).name);
  950. writer.AsmLn;
  951. end;
  952. asd_extern :
  953. begin
  954. writer.AsmWrite('EXTRN ');
  955. writer.AsmWrite(tai_directive(hp).name);
  956. writer.AsmLn;
  957. end;
  958. asd_cpu :
  959. begin
  960. if (asminfo^.id = as_i386_wasm) then
  961. begin
  962. {writer.AsmWrite('.');}
  963. for cpu:=low(tcputype) to high(tcputype) do
  964. begin
  965. if tai_directive(hp).name=CPUTypeStr[CPU] then
  966. begin
  967. { writer.AsmWriteLn(wasm_cpu_name[cpu]); }
  968. break;
  969. end;
  970. end;
  971. end
  972. else
  973. begin
  974. { TODO: implement this properly for TASM/MASM/WASM (.686p, etc.) }
  975. writer.AsmWrite(asminfo^.comment+' CPU ');
  976. writer.AsmWrite(tai_directive(hp).name);
  977. writer.AsmLn;
  978. end;
  979. end
  980. else
  981. internalerror(200509192);
  982. end;
  983. end;
  984. ait_seh_directive :
  985. { Ignore for now };
  986. else
  987. internalerror(10000);
  988. end;
  989. hp:=tai(hp.next);
  990. end;
  991. end;
  992. procedure tx86intelassembler.WriteExternals;
  993. var
  994. sym : TAsmSymbol;
  995. i : longint;
  996. begin
  997. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  998. begin
  999. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1000. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  1001. begin
  1002. case asminfo^.id of
  1003. as_i386_masm,
  1004. as_i386_wasm :
  1005. writer.AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  1006. as_x86_64_masm :
  1007. writer.AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  1008. else
  1009. writer.AsmWriteln(#9'EXTRN'#9+sym.name);
  1010. end;
  1011. end;
  1012. end;
  1013. end;
  1014. function tx86intelassembler.DoAssemble : boolean;
  1015. var
  1016. masmobjfn : string;
  1017. begin
  1018. DoAssemble:=Inherited DoAssemble;
  1019. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  1020. if (asminfo^.id in [as_i386_masm,as_i386_wasm]) then
  1021. begin
  1022. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  1023. if not(cs_asm_extern in current_settings.globalswitches) then
  1024. begin
  1025. if Not FileExists(objfilename) and
  1026. FileExists(masmobjfn) then
  1027. RenameFile(masmobjfn,objfilename);
  1028. end
  1029. else
  1030. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  1031. end;
  1032. end;
  1033. procedure tx86IntelAssembler.WriteAsmList;
  1034. var
  1035. hal : tasmlisttype;
  1036. begin
  1037. {$ifdef EXTDEBUG}
  1038. if current_module.mainsource<>'' then
  1039. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource);
  1040. {$endif}
  1041. if asminfo^.id<>as_x86_64_masm then
  1042. begin
  1043. if (asminfo^.id = as_i386_wasm) then
  1044. begin
  1045. writer.AsmWriteLn(#9'.686p');
  1046. writer.AsmWriteLn(#9'.xmm');
  1047. end
  1048. else
  1049. writer.AsmWriteLn(#9'.386p');
  1050. { masm 6.11 does not seem to like LOCALS PM }
  1051. if (asminfo^.id = as_i386_tasm) then
  1052. begin
  1053. writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix);
  1054. end;
  1055. {$ifdef i8086}
  1056. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  1057. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  1058. {$endif i8086}
  1059. writer.AsmLn;
  1060. end;
  1061. WriteExternals;
  1062. for hal:=low(TasmlistType) to high(TasmlistType) do
  1063. begin
  1064. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1065. writetree(current_asmdata.asmlists[hal]);
  1066. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1067. end;
  1068. { better do this at end of WriteTree, but then there comes a trouble with
  1069. al_const which does not have leading ait_section and thus goes out of segment }
  1070. if LastSecType <> sec_none then
  1071. begin
  1072. if asminfo^.id=as_x86_64_masm then
  1073. writer.AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS')
  1074. else
  1075. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  1076. end;
  1077. LastSecType := sec_none;
  1078. writer.AsmWriteLn(#9'END');
  1079. writer.AsmLn;
  1080. {$ifdef EXTDEBUG}
  1081. if current_module.mainsource<>'' then
  1082. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource);
  1083. {$endif EXTDEBUG}
  1084. end;
  1085. {*****************************************************************************
  1086. Initialize
  1087. *****************************************************************************}
  1088. const
  1089. {$ifdef i386}
  1090. as_i386_tasm_info : tasminfo =
  1091. (
  1092. id : as_i386_tasm;
  1093. idtxt : 'TASM';
  1094. asmbin : 'tasm';
  1095. asmcmd : '/m2 /ml $EXTRAOPT $ASM $OBJ';
  1096. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1097. flags : [af_needar,af_labelprefix_only_inside_procedure];
  1098. labelprefix : '@@';
  1099. comment : '; ';
  1100. dollarsign: '$';
  1101. );
  1102. as_i386_masm_info : tasminfo =
  1103. (
  1104. id : as_i386_masm;
  1105. idtxt : 'MASM';
  1106. asmbin : 'masm';
  1107. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1108. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1109. flags : [af_needar];
  1110. labelprefix : '@@';
  1111. comment : '; ';
  1112. dollarsign: '$';
  1113. );
  1114. as_i386_wasm_info : tasminfo =
  1115. (
  1116. id : as_i386_wasm;
  1117. idtxt : 'WASM';
  1118. asmbin : 'wasm';
  1119. asmcmd : '$ASM $EXTRAOPT -6s -fp6 -ms -zq -Fo=$OBJ';
  1120. supported_targets : [system_i386_watcom];
  1121. flags : [af_needar];
  1122. labelprefix : '@@';
  1123. comment : '; ';
  1124. dollarsign: '$';
  1125. );
  1126. {$endif i386}
  1127. {$ifdef x86_64}
  1128. as_x86_64_masm_info : tasminfo =
  1129. (
  1130. id : as_x86_64_masm;
  1131. idtxt : 'MASM';
  1132. asmbin : 'ml64';
  1133. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1134. supported_targets : [system_x86_64_win64];
  1135. flags : [af_needar];
  1136. labelprefix : '@@';
  1137. comment : '; ';
  1138. dollarsign: '$';
  1139. );
  1140. {$endif x86_64}
  1141. initialization
  1142. {$ifdef x86_64}
  1143. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  1144. {$endif x86_64}
  1145. {$ifdef i386}
  1146. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  1147. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  1148. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  1149. {$endif i386}
  1150. end.