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