agx86nsm.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for the Nasm assembler with
  4. Intel syntax for the i386+
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit agx86nsm;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cpubase,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  24. type
  25. { T386NasmAssembler }
  26. T386NasmAssembler = class(texternalassembler)
  27. private
  28. procedure WriteReference(var ref : treference);
  29. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  30. procedure WriteOper_jmp(const o:toper; op : tasmop);
  31. procedure WriteSection(atype:TAsmSectiontype;const aname:string);
  32. public
  33. procedure WriteTree(p:TAsmList);override;
  34. procedure WriteAsmList;override;
  35. procedure WriteExternals;
  36. procedure WriteSmartExternals;
  37. procedure WriteHeader;
  38. end;
  39. implementation
  40. uses
  41. cutils,globtype,globals,systems,cclasses,
  42. fmodule,finput,verbose,cpuinfo,cgbase
  43. ;
  44. type
  45. {$ifdef cpuextended}
  46. t80bitarray = array[0..9] of byte;
  47. {$endif cpuextended}
  48. t64bitarray = array[0..7] of byte;
  49. t32bitarray = array[0..3] of byte;
  50. const
  51. line_length = 64;
  52. nasm_regname_table : array[tregisterindex] of string[7] = (
  53. {r386nasm.inc contains the Nasm name of each register.}
  54. {$if defined(x86_64)}
  55. {$fatal nasm support not yet implemented for x86_64 }
  56. {$elseif defined(i386)}
  57. {$i r386nasm.inc}
  58. {$elseif defined(i8086)}
  59. {$i r8086nasm.inc}
  60. {$endif}
  61. );
  62. function nasm_regname(r:Tregister):string;
  63. var
  64. p : tregisterindex;
  65. begin
  66. p:=findreg_by_number(r);
  67. if p<>0 then
  68. result:=nasm_regname_table[p]
  69. else
  70. result:=generic_regname(r);
  71. end;
  72. function single2str(d : single) : string;
  73. var
  74. hs : string;
  75. p : longint;
  76. begin
  77. str(d,hs);
  78. { nasm expects a lowercase e }
  79. p:=pos('E',hs);
  80. if p>0 then
  81. hs[p]:='e';
  82. p:=pos('+',hs);
  83. if p>0 then
  84. delete(hs,p,1);
  85. single2str:=lower(hs);
  86. end;
  87. function double2str(d : double) : string;
  88. var
  89. hs : string;
  90. p : longint;
  91. begin
  92. str(d,hs);
  93. { nasm expects a lowercase e }
  94. p:=pos('E',hs);
  95. if p>0 then
  96. hs[p]:='e';
  97. p:=pos('+',hs);
  98. if p>0 then
  99. delete(hs,p,1);
  100. double2str:=lower(hs);
  101. end;
  102. function extended2str(e : extended) : string;
  103. var
  104. hs : string;
  105. p : longint;
  106. begin
  107. str(e,hs);
  108. { nasm expects a lowercase e }
  109. p:=pos('E',hs);
  110. if p>0 then
  111. hs[p]:='e';
  112. p:=pos('+',hs);
  113. if p>0 then
  114. delete(hs,p,1);
  115. extended2str:=lower(hs);
  116. end;
  117. { convert floating point values }
  118. { to correct endian }
  119. procedure swap64bitarray(var t: t64bitarray);
  120. var
  121. b: byte;
  122. begin
  123. b:= t[7];
  124. t[7] := t[0];
  125. t[0] := b;
  126. b := t[6];
  127. t[6] := t[1];
  128. t[1] := b;
  129. b:= t[5];
  130. t[5] := t[2];
  131. t[2] := b;
  132. b:= t[4];
  133. t[4] := t[3];
  134. t[3] := b;
  135. end;
  136. procedure swap32bitarray(var t: t32bitarray);
  137. var
  138. b: byte;
  139. begin
  140. b:= t[1];
  141. t[1]:= t[2];
  142. t[2]:= b;
  143. b:= t[0];
  144. t[0]:= t[3];
  145. t[3]:= b;
  146. end;
  147. function comp2str(d : bestreal) : string;
  148. type
  149. pdouble = ^double;
  150. var
  151. c : comp;
  152. dd : pdouble;
  153. begin
  154. c:=comp(d);
  155. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  156. comp2str:=double2str(dd^);
  157. end;
  158. function sizestr(s:topsize;dest:boolean):string;
  159. begin
  160. case s of
  161. S_B : sizestr:='byte ';
  162. S_W : sizestr:='word ';
  163. S_L : sizestr:='dword ';
  164. S_Q : sizestr:='qword ';
  165. S_IS : sizestr:='word ';
  166. S_IL : sizestr:='dword ';
  167. S_IQ : sizestr:='qword ';
  168. S_FS : sizestr:='dword ';
  169. S_FL : sizestr:='qword ';
  170. S_FX : sizestr:='tword ';
  171. S_BW : if dest then
  172. sizestr:='word '
  173. else
  174. sizestr:='byte ';
  175. S_BL : if dest then
  176. sizestr:='dword '
  177. else
  178. sizestr:='byte ';
  179. S_WL : if dest then
  180. sizestr:='dword '
  181. else
  182. sizestr:='word ';
  183. else { S_NO }
  184. sizestr:='';
  185. end;
  186. end;
  187. Function PadTabs(const p:string;addch:char):string;
  188. var
  189. s : string;
  190. i : longint;
  191. begin
  192. i:=length(p);
  193. if addch<>#0 then
  194. begin
  195. inc(i);
  196. s:=p+addch;
  197. end
  198. else
  199. s:=p;
  200. if i<8 then
  201. PadTabs:=s+#9#9
  202. else
  203. PadTabs:=s+#9;
  204. end;
  205. type
  206. PExternChain = ^TExternChain;
  207. TExternChain = Record
  208. psym : pshortstring;
  209. is_defined : boolean;
  210. next : PExternChain;
  211. end;
  212. const
  213. FEC : PExternChain = nil;
  214. procedure AddSymbol(symname : string; defined : boolean);
  215. var
  216. EC : PExternChain;
  217. begin
  218. EC:=FEC;
  219. while assigned(EC) do
  220. begin
  221. if EC^.psym^=symname then
  222. begin
  223. if defined then
  224. EC^.is_defined:=true;
  225. exit;
  226. end;
  227. EC:=EC^.next;
  228. end;
  229. New(EC);
  230. EC^.next:=FEC;
  231. FEC:=EC;
  232. FEC^.psym:=stringdup(symname);
  233. FEC^.is_defined := defined;
  234. end;
  235. procedure FreeExternChainList;
  236. var
  237. EC : PExternChain;
  238. begin
  239. EC:=FEC;
  240. while assigned(EC) do
  241. begin
  242. FEC:=EC^.next;
  243. stringdispose(EC^.psym);
  244. Dispose(EC);
  245. EC:=FEC;
  246. end;
  247. end;
  248. {****************************************************************************
  249. T386NasmAssembler
  250. ****************************************************************************}
  251. procedure T386NasmAssembler.WriteReference(var ref : treference);
  252. var
  253. first : boolean;
  254. begin
  255. with ref do
  256. begin
  257. AsmWrite('[');
  258. first:=true;
  259. if (segment<>NR_NO) then
  260. AsmWrite(nasm_regname(segment)+':');
  261. if assigned(symbol) then
  262. begin
  263. AsmWrite(symbol.name);
  264. if SmartAsm then
  265. AddSymbol(symbol.name,false);
  266. first:=false;
  267. end;
  268. if (base<>NR_NO) then
  269. begin
  270. if not(first) then
  271. AsmWrite('+')
  272. else
  273. first:=false;
  274. AsmWrite(nasm_regname(base))
  275. end;
  276. if (index<>NR_NO) then
  277. begin
  278. if not(first) then
  279. AsmWrite('+')
  280. else
  281. first:=false;
  282. AsmWrite(nasm_regname(index));
  283. if scalefactor<>0 then
  284. AsmWrite('*'+tostr(scalefactor));
  285. end;
  286. if offset<0 then
  287. begin
  288. AsmWrite(tostr(offset));
  289. first:=false;
  290. end
  291. else if (offset>0) then
  292. begin
  293. AsmWrite('+'+tostr(offset));
  294. first:=false;
  295. end;
  296. if first then
  297. AsmWrite('0');
  298. AsmWrite(']');
  299. end;
  300. end;
  301. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  302. begin
  303. case o.typ of
  304. top_reg :
  305. AsmWrite(nasm_regname(o.reg));
  306. top_const :
  307. begin
  308. if (ops=1) and (opcode<>A_RET) then
  309. AsmWrite(sizestr(s,dest));
  310. AsmWrite(tostr(longint(o.val)));
  311. end;
  312. top_ref :
  313. begin
  314. if o.ref^.refaddr=addr_no then
  315. begin
  316. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  317. (opcode = A_LSS) or (opcode = A_LFS) or
  318. (opcode = A_LES) or (opcode = A_LDS) or
  319. // (opcode = A_SHR) or (opcode = A_SHL) or
  320. // (opcode = A_SAR) or (opcode = A_SAL) or
  321. (opcode = A_OUT) or (opcode = A_IN)) then
  322. AsmWrite(sizestr(s,dest));
  323. WriteReference(o.ref^);
  324. end
  325. {$ifdef i8086}
  326. else if o.ref^.refaddr=addr_dgroup then
  327. begin
  328. AsmWrite('dgroup');
  329. end
  330. {$endif i8086}
  331. else
  332. begin
  333. {$ifdef x86_64}
  334. asmwrite('qword ');
  335. {$endif}
  336. {$ifdef i386}
  337. asmwrite('dword ');
  338. {$endif i386}
  339. {$ifdef i8086}
  340. asmwrite('word ');
  341. {$endif i8086}
  342. if assigned(o.ref^.symbol) then
  343. begin
  344. if SmartAsm then
  345. AddSymbol(o.ref^.symbol.name,false);
  346. asmwrite(o.ref^.symbol.name);
  347. if o.ref^.offset=0 then
  348. exit;
  349. end;
  350. if o.ref^.offset>0 then
  351. asmwrite('+');
  352. asmwrite(tostr(o.ref^.offset));
  353. end;
  354. end;
  355. else
  356. internalerror(10001);
  357. end;
  358. end;
  359. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  360. begin
  361. case o.typ of
  362. top_reg :
  363. AsmWrite(nasm_regname(o.reg));
  364. top_ref :
  365. if o.ref^.refaddr=addr_no then
  366. WriteReference(o.ref^)
  367. else
  368. begin
  369. { NEAR forces NASM to emit near jumps, which are 386+ }
  370. {$ifndef i8086}
  371. if not(
  372. (op=A_JCXZ) or (op=A_JECXZ) or
  373. {$ifdef x86_64}
  374. (op=A_JRCXZ) or
  375. {$endif x86_64}
  376. (op=A_LOOP) or (op=A_LOOPE) or
  377. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  378. (op=A_LOOPZ)
  379. ) then
  380. AsmWrite('NEAR ');
  381. {$endif i8086}
  382. AsmWrite(o.ref^.symbol.name);
  383. if SmartAsm then
  384. AddSymbol(o.ref^.symbol.name,false);
  385. if o.ref^.offset>0 then
  386. AsmWrite('+'+tostr(o.ref^.offset))
  387. else
  388. if o.ref^.offset<0 then
  389. AsmWrite(tostr(o.ref^.offset));
  390. end;
  391. top_const :
  392. AsmWrite(tostr(aint(o.val)));
  393. else
  394. internalerror(10001);
  395. end;
  396. end;
  397. const
  398. ait_const2str : array[aitconst_128bit..aitconst_secrel32_symbol] of string[20]=(
  399. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  400. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  401. #9'RVA'#9,#9'SECREL32'#9
  402. );
  403. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  404. const
  405. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  406. '.text',
  407. '.data',
  408. '.data',
  409. '.rodata',
  410. '.bss',
  411. '.tbss',
  412. '.pdata',
  413. '.text','.data','.data','.data','.data',
  414. '.stab',
  415. '.stabstr',
  416. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  417. '.eh_frame',
  418. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  419. '.fpc',
  420. '',
  421. '.init',
  422. '.fini',
  423. '.objc_class',
  424. '.objc_meta_class',
  425. '.objc_cat_cls_meth',
  426. '.objc_cat_inst_meth',
  427. '.objc_protocol',
  428. '.objc_string_object',
  429. '.objc_cls_meth',
  430. '.objc_inst_meth',
  431. '.objc_cls_refs',
  432. '.objc_message_refs',
  433. '.objc_symbols',
  434. '.objc_category',
  435. '.objc_class_vars',
  436. '.objc_instance_vars',
  437. '.objc_module_info',
  438. '.objc_class_names',
  439. '.objc_meth_var_types',
  440. '.objc_meth_var_names',
  441. '.objc_selector_strs',
  442. '.objc_protocol_ext',
  443. '.objc_class_ext',
  444. '.objc_property',
  445. '.objc_image_info',
  446. '.objc_cstring_object',
  447. '.objc_sel_fixup',
  448. '__DATA,__objc_data',
  449. '__DATA,__objc_const',
  450. '.objc_superrefs',
  451. '__DATA, __datacoal_nt,coalesced',
  452. '.objc_classlist',
  453. '.objc_nlclasslist',
  454. '.objc_catlist',
  455. '.obcj_nlcatlist',
  456. '.objc_protolist'
  457. );
  458. begin
  459. AsmLn;
  460. AsmWrite('SECTION ');
  461. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  462. Thus, data which normally goes into .rodata and .rodata_norel sections must
  463. end up in .data section }
  464. if (atype in [sec_rodata,sec_rodata_norel]) and
  465. (target_info.system=system_i386_go32v2) then
  466. AsmWrite('.data')
  467. else
  468. AsmWrite(secnames[atype]);
  469. if create_smartlink_sections and
  470. (atype<>sec_bss) and
  471. (aname<>'') then
  472. begin
  473. AsmWrite('.');
  474. AsmWrite(aname);
  475. end;
  476. AsmLn;
  477. LasTSecType:=atype;
  478. end;
  479. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  480. {$ifdef cpuextended}
  481. type
  482. t80bitarray = array[0..9] of byte;
  483. {$endif cpuextended}
  484. var
  485. s : string;
  486. hp : tai;
  487. counter,
  488. lines,
  489. i,j,l : longint;
  490. InlineLevel : longint;
  491. consttype : taiconst_type;
  492. do_line,
  493. quoted : boolean;
  494. co : comp;
  495. sin : single;
  496. d : double;
  497. {$ifdef cpuextended}
  498. e : extended;
  499. {$endif cpuextended}
  500. fixed_opcode: TAsmOp;
  501. begin
  502. if not assigned(p) then
  503. exit;
  504. InlineLevel:=0;
  505. { lineinfo is only needed for al_procedures (PFV) }
  506. do_line:=(cs_asm_source in current_settings.globalswitches) or
  507. ((cs_lineinfo in current_settings.moduleswitches)
  508. and (p=current_asmdata.asmlists[al_procedures]));
  509. hp:=tai(p.first);
  510. while assigned(hp) do
  511. begin
  512. prefetch(pointer(hp.next)^);
  513. if not(hp.typ in SkipLineInfo) then
  514. begin
  515. current_filepos:=tailineinfo(hp).fileinfo;
  516. { no line info for inlined code }
  517. if do_line and (inlinelevel=0) then
  518. WriteSourceLine(hp as tailineinfo);
  519. end;
  520. case hp.typ of
  521. ait_comment :
  522. Begin
  523. AsmWrite(target_asm.comment);
  524. AsmWritePChar(tai_comment(hp).str);
  525. AsmLn;
  526. End;
  527. ait_regalloc :
  528. begin
  529. if (cs_asm_regalloc in current_settings.globalswitches) then
  530. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+' '+
  531. regallocstr[tai_regalloc(hp).ratype]);
  532. end;
  533. ait_tempalloc :
  534. begin
  535. if (cs_asm_tempalloc in current_settings.globalswitches) then
  536. WriteTempalloc(tai_tempalloc(hp));
  537. end;
  538. ait_section :
  539. begin
  540. if tai_section(hp).sectype<>sec_none then
  541. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  542. LasTSecType:=tai_section(hp).sectype;
  543. end;
  544. ait_align :
  545. begin
  546. { nasm gives warnings when it finds align in bss as it
  547. wants to store data }
  548. if (lastsectype<>sec_bss) and
  549. (tai_align(hp).aligntype>1) then
  550. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  551. end;
  552. ait_datablock :
  553. begin
  554. if tai_datablock(hp).is_global or SmartAsm then
  555. begin
  556. AsmWrite(#9'GLOBAL ');
  557. AsmWriteLn(tai_datablock(hp).sym.name);
  558. end;
  559. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  560. if SmartAsm then
  561. AddSymbol(tai_datablock(hp).sym.name,true);
  562. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  563. end;
  564. ait_const:
  565. begin
  566. consttype:=tai_const(hp).consttype;
  567. case consttype of
  568. aitconst_64bit :
  569. begin
  570. if assigned(tai_const(hp).sym) then
  571. internalerror(200404292);
  572. AsmWrite(ait_const2str[aitconst_32bit]);
  573. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  574. AsmWrite(',');
  575. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  576. AsmLn;
  577. end;
  578. aitconst_uleb128bit,
  579. aitconst_sleb128bit,
  580. aitconst_128bit:
  581. begin
  582. end;
  583. aitconst_32bit,
  584. aitconst_16bit,
  585. aitconst_8bit,
  586. aitconst_rva_symbol,
  587. aitconst_secrel32_symbol :
  588. begin
  589. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  590. l:=0;
  591. repeat
  592. if assigned(tai_const(hp).sym) then
  593. begin
  594. if SmartAsm then
  595. begin
  596. AddSymbol(tai_const(hp).sym.name,false);
  597. if assigned(tai_const(hp).endsym) then
  598. AddSymbol(tai_const(hp).endsym.name,false);
  599. end;
  600. if assigned(tai_const(hp).endsym) then
  601. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  602. else
  603. s:=tai_const(hp).sym.name;
  604. if tai_const(hp).value<>0 then
  605. s:=s+tostr_with_plus(tai_const(hp).value);
  606. end
  607. else
  608. s:=tostr(tai_const(hp).value);
  609. AsmWrite(s);
  610. inc(l,length(s));
  611. if (l>line_length) or
  612. (hp.next=nil) or
  613. (tai(hp.next).typ<>ait_const) or
  614. (tai_const(hp.next).consttype<>consttype) then
  615. break;
  616. hp:=tai(hp.next);
  617. AsmWrite(',');
  618. until false;
  619. AsmLn;
  620. end;
  621. else
  622. internalerror(200704252);
  623. end;
  624. end;
  625. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  626. ait_real_80bit :
  627. begin
  628. if do_line then
  629. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  630. { Make sure e is a extended type, bestreal could be
  631. a different type (bestreal) !! (PFV) }
  632. e:=tai_real_80bit(hp).value;
  633. AsmWrite(#9#9'DB'#9);
  634. for i:=0 to 9 do
  635. begin
  636. if i<>0 then
  637. AsmWrite(',');
  638. AsmWrite(tostr(t80bitarray(e)[i]));
  639. end;
  640. for i:=11 to tai_real_80bit(hp).savesize do
  641. AsmWrite(',0');
  642. AsmLn;
  643. end;
  644. {$else cpuextended}
  645. ait_real_80bit :
  646. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  647. {$endif cpuextended}
  648. // ait_real_64bit :
  649. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  650. ait_real_64bit :
  651. begin
  652. if do_line then
  653. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  654. d:=tai_real_64bit(hp).value;
  655. { swap the values to correct endian if required }
  656. if source_info.endian <> target_info.endian then
  657. swap64bitarray(t64bitarray(d));
  658. AsmWrite(#9#9'DB'#9);
  659. {$ifdef arm}
  660. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  661. begin
  662. for i:=4 to 7 do
  663. begin
  664. if i<>4 then
  665. AsmWrite(',');
  666. AsmWrite(tostr(t64bitarray(d)[i]));
  667. end;
  668. for i:=0 to 3 do
  669. begin
  670. AsmWrite(',');
  671. AsmWrite(tostr(t64bitarray(d)[i]));
  672. end;
  673. end
  674. else
  675. {$endif arm}
  676. begin
  677. for i:=0 to 7 do
  678. begin
  679. if i<>0 then
  680. AsmWrite(',');
  681. AsmWrite(tostr(t64bitarray(d)[i]));
  682. end;
  683. end;
  684. AsmLn;
  685. end;
  686. // ait_real_32bit :
  687. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  688. ait_real_32bit :
  689. begin
  690. if do_line then
  691. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  692. sin:=tai_real_32bit(hp).value;
  693. { swap the values to correct endian if required }
  694. if source_info.endian <> target_info.endian then
  695. swap32bitarray(t32bitarray(sin));
  696. AsmWrite(#9#9'DB'#9);
  697. for i:=0 to 3 do
  698. begin
  699. if i<>0 then
  700. AsmWrite(',');
  701. AsmWrite(tostr(t32bitarray(sin)[i]));
  702. end;
  703. AsmLn;
  704. end;
  705. // ait_comp_64bit :
  706. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  707. ait_comp_64bit :
  708. begin
  709. if do_line then
  710. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  711. AsmWrite(#9#9'DB'#9);
  712. co:=comp(tai_comp_64bit(hp).value);
  713. { swap the values to correct endian if required }
  714. if source_info.endian <> target_info.endian then
  715. swap64bitarray(t64bitarray(co));
  716. for i:=0 to 7 do
  717. begin
  718. if i<>0 then
  719. AsmWrite(',');
  720. AsmWrite(tostr(t64bitarray(co)[i]));
  721. end;
  722. AsmLn;
  723. end;
  724. ait_string :
  725. begin
  726. counter := 0;
  727. lines := tai_string(hp).len div line_length;
  728. { separate lines in different parts }
  729. if tai_string(hp).len > 0 then
  730. Begin
  731. for j := 0 to lines-1 do
  732. begin
  733. AsmWrite(#9#9'DB'#9);
  734. quoted:=false;
  735. for i:=counter to counter+line_length-1 do
  736. begin
  737. { it is an ascii character. }
  738. if (ord(tai_string(hp).str[i])>31) and
  739. (ord(tai_string(hp).str[i])<128) and
  740. (tai_string(hp).str[i]<>'"') then
  741. begin
  742. if not(quoted) then
  743. begin
  744. if i>counter then
  745. AsmWrite(',');
  746. AsmWrite('"');
  747. end;
  748. AsmWrite(tai_string(hp).str[i]);
  749. quoted:=true;
  750. end { if > 31 and < 128 and ord('"') }
  751. else
  752. begin
  753. if quoted then
  754. AsmWrite('"');
  755. if i>counter then
  756. AsmWrite(',');
  757. quoted:=false;
  758. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  759. end;
  760. end; { end for i:=0 to... }
  761. if quoted then AsmWrite('"');
  762. AsmWrite(target_info.newline);
  763. inc(counter,line_length);
  764. end; { end for j:=0 ... }
  765. { do last line of lines }
  766. if counter<tai_string(hp).len then
  767. AsmWrite(#9#9'DB'#9);
  768. quoted:=false;
  769. for i:=counter to tai_string(hp).len-1 do
  770. begin
  771. { it is an ascii character. }
  772. if (ord(tai_string(hp).str[i])>31) and
  773. (ord(tai_string(hp).str[i])<128) and
  774. (tai_string(hp).str[i]<>'"') then
  775. begin
  776. if not(quoted) then
  777. begin
  778. if i>counter then
  779. AsmWrite(',');
  780. AsmWrite('"');
  781. end;
  782. AsmWrite(tai_string(hp).str[i]);
  783. quoted:=true;
  784. end { if > 31 and < 128 and " }
  785. else
  786. begin
  787. if quoted then
  788. AsmWrite('"');
  789. if i>counter then
  790. AsmWrite(',');
  791. quoted:=false;
  792. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  793. end;
  794. end; { end for i:=0 to... }
  795. if quoted then
  796. AsmWrite('"');
  797. end;
  798. AsmLn;
  799. end;
  800. ait_label :
  801. begin
  802. if tai_label(hp).labsym.is_used then
  803. begin
  804. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  805. begin
  806. AsmWrite(#9'GLOBAL ');
  807. AsmWriteLn(tai_label(hp).labsym.name);
  808. end;
  809. AsmWriteLn(tai_label(hp).labsym.name+':');
  810. end;
  811. if SmartAsm then
  812. AddSymbol(tai_label(hp).labsym.name,true);
  813. end;
  814. ait_symbol :
  815. begin
  816. if tai_symbol(hp).has_value then
  817. internalerror(2009090803);
  818. if tai_symbol(hp).is_global or SmartAsm then
  819. begin
  820. AsmWrite(#9'GLOBAL ');
  821. AsmWriteLn(tai_symbol(hp).sym.name);
  822. end;
  823. AsmWrite(tai_symbol(hp).sym.name);
  824. if SmartAsm then
  825. AddSymbol(tai_symbol(hp).sym.name,true);
  826. if assigned(hp.next) and not(tai(hp.next).typ in
  827. [ait_const,
  828. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  829. AsmWriteLn(':')
  830. end;
  831. ait_symbol_end : ;
  832. ait_instruction :
  833. begin
  834. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  835. { We need intel order, no At&t }
  836. taicpu(hp).SetOperandOrder(op_intel);
  837. s:='';
  838. if ((fixed_opcode=A_FADDP) or
  839. (fixed_opcode=A_FMULP))
  840. and (taicpu(hp).ops=0) then
  841. begin
  842. taicpu(hp).allocate_oper(2);
  843. taicpu(hp).oper[0]^.typ:=top_reg;
  844. taicpu(hp).oper[0]^.reg:=NR_ST1;
  845. taicpu(hp).oper[1]^.typ:=top_reg;
  846. taicpu(hp).oper[1]^.reg:=NR_ST;
  847. end;
  848. if fixed_opcode=A_FWAIT then
  849. AsmWriteln(#9#9'DB'#9'09bh')
  850. else
  851. begin
  852. {$ifndef i8086}
  853. { We need to explicitely set
  854. word prefix to get selectors
  855. to be pushed in 2 bytes PM }
  856. if (taicpu(hp).opsize=S_W) and
  857. ((fixed_opcode=A_PUSH) or
  858. (fixed_opcode=A_POP)) and
  859. (taicpu(hp).oper[0]^.typ=top_reg) and
  860. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  861. AsmWriteln(#9#9'DB'#9'066h');
  862. {$endif not i8086}
  863. AsmWrite(#9#9+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  864. if taicpu(hp).ops<>0 then
  865. begin
  866. if is_calljmp(fixed_opcode) then
  867. begin
  868. AsmWrite(#9);
  869. WriteOper_jmp(taicpu(hp).oper[0]^,fixed_opcode);
  870. end
  871. else
  872. begin
  873. for i:=0 to taicpu(hp).ops-1 do
  874. begin
  875. if i=0 then
  876. AsmWrite(#9)
  877. else
  878. AsmWrite(',');
  879. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  880. end;
  881. end;
  882. end;
  883. AsmLn;
  884. end;
  885. end;
  886. ait_stab,
  887. ait_force_line,
  888. ait_function_name : ;
  889. ait_cutobject :
  890. begin
  891. if SmartAsm then
  892. begin
  893. { only reset buffer if nothing has changed }
  894. if AsmSize=AsmStartSize then
  895. AsmClear
  896. else
  897. begin
  898. if SmartAsm then
  899. begin
  900. WriteSmartExternals;
  901. FreeExternChainList;
  902. end;
  903. AsmClose;
  904. DoAssemble;
  905. AsmCreate(tai_cutobject(hp).place);
  906. WriteHeader;
  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 lasTSectype<>sec_none then
  916. WriteSection(lasTSectype,'');
  917. AsmStartSize:=AsmSize;
  918. end;
  919. end;
  920. ait_marker :
  921. if tai_marker(hp).kind=mark_NoLineInfoStart then
  922. inc(InlineLevel)
  923. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  924. dec(InlineLevel);
  925. ait_directive :
  926. begin
  927. case tai_directive(hp).directive of
  928. asd_nasm_import :
  929. AsmWrite('import ');
  930. asd_extern :
  931. AsmWrite('EXTERN ');
  932. else
  933. internalerror(200509191);
  934. end;
  935. if tai_directive(hp).name<>'' then
  936. begin
  937. if SmartAsm then
  938. AddSymbol(tai_directive(hp).name,false);
  939. AsmWrite(tai_directive(hp).name);
  940. end;
  941. AsmLn;
  942. end;
  943. ait_seh_directive :
  944. { Ignore for now };
  945. else
  946. internalerror(10000);
  947. end;
  948. hp:=tai(hp.next);
  949. end;
  950. end;
  951. procedure T386NasmAssembler.WriteExternals;
  952. var
  953. sym : TAsmSymbol;
  954. i : longint;
  955. begin
  956. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  957. begin
  958. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  959. if sym.bind=AB_EXTERNAL then
  960. AsmWriteln('EXTERN'#9+sym.name);
  961. end;
  962. end;
  963. procedure T386NasmAssembler.WriteSmartExternals;
  964. var
  965. EC : PExternChain;
  966. begin
  967. EC:=FEC;
  968. while assigned(EC) do
  969. begin
  970. if not EC^.is_defined then
  971. AsmWriteln('EXTERN'#9+EC^.psym^);
  972. EC:=EC^.next;
  973. end;
  974. end;
  975. procedure T386NasmAssembler.WriteHeader;
  976. begin
  977. {$ifdef i8086}
  978. AsmWriteLn('BITS 16');
  979. case current_settings.cputype of
  980. cpu_8086: AsmWriteLn('CPU 8086');
  981. cpu_186: AsmWriteLn('CPU 186');
  982. cpu_286: AsmWriteLn('CPU 286');
  983. cpu_386: AsmWriteLn('CPU 386');
  984. cpu_Pentium: AsmWriteLn('CPU PENTIUM');
  985. cpu_Pentium2: AsmWriteLn('CPU P2');
  986. cpu_Pentium3: AsmWriteLn('CPU P3');
  987. cpu_Pentium4: AsmWriteLn('CPU P4');
  988. cpu_PentiumM: AsmWriteLn('CPU P4');
  989. else
  990. internalerror(2013050101);
  991. end;
  992. { NASM complains if you put a missing section in the GROUP directive, so }
  993. { we add empty declarations to make sure they exist, even if empty }
  994. AsmWriteLn('SECTION .rodata');
  995. AsmWriteLn('SECTION .data');
  996. { WLINK requires class=bss in order to leave the BSS section out of the executable }
  997. AsmWriteLn('SECTION .bss class=bss');
  998. { group these sections in the same segment }
  999. AsmWriteLn('GROUP dgroup rodata data bss');
  1000. AsmWriteLn('SECTION .text');
  1001. {$else i8086}
  1002. AsmWriteLn('BITS 32');
  1003. {$endif i8086}
  1004. end;
  1005. procedure T386NasmAssembler.WriteAsmList;
  1006. var
  1007. hal : tasmlisttype;
  1008. begin
  1009. {$ifdef EXTDEBUG}
  1010. if current_module.mainsource<>'' then
  1011. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1012. {$endif}
  1013. WriteHeader;
  1014. AsmLn;
  1015. WriteExternals;
  1016. for hal:=low(TasmlistType) to high(TasmlistType) do
  1017. begin
  1018. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1019. writetree(current_asmdata.asmlists[hal]);
  1020. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  1021. end;
  1022. AsmLn;
  1023. if SmartAsm then
  1024. begin
  1025. WriteSmartExternals;
  1026. FreeExternChainList;
  1027. end;
  1028. {$ifdef EXTDEBUG}
  1029. if current_module.mainsource<>'' then
  1030. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1031. {$endif EXTDEBUG}
  1032. end;
  1033. {*****************************************************************************
  1034. Initialize
  1035. *****************************************************************************}
  1036. const
  1037. as_i386_nasmcoff_info : tasminfo =
  1038. (
  1039. id : as_i386_nasmcoff;
  1040. idtxt : 'NASMCOFF';
  1041. asmbin : 'nasm';
  1042. asmcmd : '-f coff -o $OBJ $ASM';
  1043. supported_targets : [system_i386_go32v2];
  1044. flags : [af_needar,af_no_debug];
  1045. labelprefix : '..@';
  1046. comment : '; ';
  1047. dollarsign: '$';
  1048. );
  1049. as_i386_nasmwin32_info : tasminfo =
  1050. (
  1051. id : as_i386_nasmwin32;
  1052. idtxt : 'NASMWIN32';
  1053. asmbin : 'nasm';
  1054. asmcmd : '-f win32 -o $OBJ $ASM';
  1055. supported_targets : [system_i386_win32];
  1056. flags : [af_needar,af_no_debug];
  1057. labelprefix : '..@';
  1058. comment : '; ';
  1059. dollarsign: '$';
  1060. );
  1061. as_i386_nasmobj_info : tasminfo =
  1062. (
  1063. id : as_i386_nasmobj;
  1064. idtxt : 'NASMOBJ';
  1065. asmbin : 'nasm';
  1066. asmcmd : '-f obj -o $OBJ $ASM';
  1067. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1068. flags : [af_needar,af_no_debug];
  1069. labelprefix : '..@';
  1070. comment : '; ';
  1071. dollarsign: '$';
  1072. );
  1073. as_i386_nasmwdosx_info : tasminfo =
  1074. (
  1075. id : as_i386_nasmwdosx;
  1076. idtxt : 'NASMWDOSX';
  1077. asmbin : 'nasm';
  1078. asmcmd : '-f win32 -o $OBJ $ASM';
  1079. supported_targets : [system_i386_wdosx];
  1080. flags : [af_needar,af_no_debug];
  1081. labelprefix : '..@';
  1082. comment : '; ';
  1083. dollarsign: '$';
  1084. );
  1085. as_i386_nasmelf_info : tasminfo =
  1086. (
  1087. id : as_i386_nasmelf;
  1088. idtxt : 'NASMELF';
  1089. asmbin : 'nasm';
  1090. asmcmd : '-f elf -o $OBJ $ASM';
  1091. supported_targets : [system_i386_linux];
  1092. flags : [af_needar,af_no_debug];
  1093. labelprefix : '..@';
  1094. comment : '; ';
  1095. dollarsign: '$';
  1096. );
  1097. as_i386_nasmbeos_info : tasminfo =
  1098. (
  1099. id : as_i386_nasmbeos;
  1100. idtxt : 'NASMELF';
  1101. asmbin : 'nasm';
  1102. asmcmd : '-f elf -o $OBJ $ASM';
  1103. supported_targets : [system_i386_beos];
  1104. flags : [af_needar,af_no_debug];
  1105. labelprefix : '..@';
  1106. comment : '; ';
  1107. dollarsign: '$';
  1108. );
  1109. as_i386_nasmhaiku_info : tasminfo =
  1110. (
  1111. id : as_i386_nasmhaiku;
  1112. idtxt : 'NASMELF';
  1113. asmbin : 'nasm';
  1114. asmcmd : '-f elf -o $OBJ $ASM';
  1115. supported_targets : [system_i386_haiku];
  1116. flags : [af_needar,af_no_debug];
  1117. labelprefix : '..@';
  1118. comment : '; ';
  1119. dollarsign: '$';
  1120. );
  1121. initialization
  1122. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1123. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1124. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1125. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1126. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1127. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1128. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1129. end.