agx86int.pas 38 KB

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