agx86nsm.pas 37 KB

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