agx86nsm.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. asmwrite('dword ');
  320. if assigned(o.ref^.symbol) then
  321. begin
  322. if SmartAsm then
  323. AddSymbol(o.ref^.symbol.name,false);
  324. asmwrite(o.ref^.symbol.name);
  325. if o.ref^.offset=0 then
  326. exit;
  327. end;
  328. if o.ref^.offset>0 then
  329. asmwrite('+');
  330. asmwrite(tostr(o.ref^.offset));
  331. end;
  332. end;
  333. else
  334. internalerror(10001);
  335. end;
  336. end;
  337. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  338. begin
  339. case o.typ of
  340. top_reg :
  341. AsmWrite(nasm_regname(o.reg));
  342. top_ref :
  343. if o.ref^.refaddr=addr_no then
  344. WriteReference(o.ref^)
  345. else
  346. begin
  347. if not(
  348. (op=A_JCXZ) or (op=A_JECXZ) or
  349. {$ifdef x86_64}
  350. (op=A_JRCXZ) or
  351. {$endif x86_64}
  352. (op=A_LOOP) or (op=A_LOOPE) or
  353. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  354. (op=A_LOOPZ)
  355. ) then
  356. AsmWrite('NEAR ');
  357. AsmWrite(o.ref^.symbol.name);
  358. if SmartAsm then
  359. AddSymbol(o.ref^.symbol.name,false);
  360. if o.ref^.offset>0 then
  361. AsmWrite('+'+tostr(o.ref^.offset))
  362. else
  363. if o.ref^.offset<0 then
  364. AsmWrite(tostr(o.ref^.offset));
  365. end;
  366. top_const :
  367. AsmWrite(tostr(aint(o.val)));
  368. else
  369. internalerror(10001);
  370. end;
  371. end;
  372. const
  373. ait_const2str : array[aitconst_128bit..aitconst_secrel32_symbol] of string[20]=(
  374. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  375. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  376. #9'RVA'#9,#9'SECREL32'#9
  377. );
  378. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  379. const
  380. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  381. '.text',
  382. '.data',
  383. '.data',
  384. '.rodata',
  385. '.bss',
  386. '.tbss',
  387. '.pdata',
  388. '.text','.data','.data','.data','.data',
  389. '.stab',
  390. '.stabstr',
  391. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  392. '.eh_frame',
  393. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  394. '.fpc',
  395. '',
  396. '.init',
  397. '.fini',
  398. '.objc_class',
  399. '.objc_meta_class',
  400. '.objc_cat_cls_meth',
  401. '.objc_cat_inst_meth',
  402. '.objc_protocol',
  403. '.objc_string_object',
  404. '.objc_cls_meth',
  405. '.objc_inst_meth',
  406. '.objc_cls_refs',
  407. '.objc_message_refs',
  408. '.objc_symbols',
  409. '.objc_category',
  410. '.objc_class_vars',
  411. '.objc_instance_vars',
  412. '.objc_module_info',
  413. '.objc_class_names',
  414. '.objc_meth_var_types',
  415. '.objc_meth_var_names',
  416. '.objc_selector_strs',
  417. '.objc_protocol_ext',
  418. '.objc_class_ext',
  419. '.objc_property',
  420. '.objc_image_info',
  421. '.objc_cstring_object',
  422. '.objc_sel_fixup',
  423. '__DATA,__objc_data',
  424. '__DATA,__objc_const',
  425. '.objc_superrefs',
  426. '__DATA, __datacoal_nt,coalesced',
  427. '.objc_classlist',
  428. '.objc_nlclasslist',
  429. '.objc_catlist',
  430. '.obcj_nlcatlist',
  431. '.objc_protolist'
  432. );
  433. begin
  434. AsmLn;
  435. AsmWrite('SECTION ');
  436. AsmWrite(secnames[atype]);
  437. if create_smartlink_sections and
  438. (atype<>sec_bss) and
  439. (aname<>'') then
  440. begin
  441. AsmWrite('.');
  442. AsmWrite(aname);
  443. end;
  444. AsmLn;
  445. LasTSecType:=atype;
  446. end;
  447. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  448. {$ifdef cpuextended}
  449. type
  450. t80bitarray = array[0..9] of byte;
  451. {$endif cpuextended}
  452. var
  453. s : string;
  454. hp : tai;
  455. counter,
  456. lines,
  457. i,j,l : longint;
  458. InlineLevel : longint;
  459. consttype : taiconst_type;
  460. do_line,
  461. quoted : boolean;
  462. co : comp;
  463. sin : single;
  464. d : double;
  465. {$ifdef cpuextended}
  466. e : extended;
  467. {$endif cpuextended}
  468. begin
  469. if not assigned(p) then
  470. exit;
  471. InlineLevel:=0;
  472. { lineinfo is only needed for al_procedures (PFV) }
  473. do_line:=(cs_asm_source in current_settings.globalswitches) or
  474. ((cs_lineinfo in current_settings.moduleswitches)
  475. and (p=current_asmdata.asmlists[al_procedures]));
  476. hp:=tai(p.first);
  477. while assigned(hp) do
  478. begin
  479. prefetch(pointer(hp.next)^);
  480. if not(hp.typ in SkipLineInfo) then
  481. begin
  482. current_filepos:=tailineinfo(hp).fileinfo;
  483. { no line info for inlined code }
  484. if do_line and (inlinelevel=0) then
  485. WriteSourceLine(hp as tailineinfo);
  486. end;
  487. case hp.typ of
  488. ait_comment :
  489. Begin
  490. AsmWrite(target_asm.comment);
  491. AsmWritePChar(tai_comment(hp).str);
  492. AsmLn;
  493. End;
  494. ait_regalloc :
  495. begin
  496. if (cs_asm_regalloc in current_settings.globalswitches) then
  497. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  498. regallocstr[tai_regalloc(hp).ratype]);
  499. end;
  500. ait_tempalloc :
  501. begin
  502. if (cs_asm_tempalloc in current_settings.globalswitches) then
  503. WriteTempalloc(tai_tempalloc(hp));
  504. end;
  505. ait_section :
  506. begin
  507. if tai_section(hp).sectype<>sec_none then
  508. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  509. LasTSecType:=tai_section(hp).sectype;
  510. end;
  511. ait_align :
  512. begin
  513. { nasm gives warnings when it finds align in bss as it
  514. wants to store data }
  515. if (lastsectype<>sec_bss) and
  516. (tai_align(hp).aligntype>1) then
  517. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  518. end;
  519. ait_datablock :
  520. begin
  521. if tai_datablock(hp).is_global then
  522. begin
  523. AsmWrite(#9'GLOBAL ');
  524. AsmWriteLn(tai_datablock(hp).sym.name);
  525. end;
  526. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  527. if SmartAsm then
  528. AddSymbol(tai_datablock(hp).sym.name,true);
  529. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  530. end;
  531. ait_const:
  532. begin
  533. consttype:=tai_const(hp).consttype;
  534. case consttype of
  535. aitconst_64bit :
  536. begin
  537. if assigned(tai_const(hp).sym) then
  538. internalerror(200404292);
  539. AsmWrite(ait_const2str[aitconst_32bit]);
  540. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  541. AsmWrite(',');
  542. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  543. AsmLn;
  544. end;
  545. aitconst_uleb128bit,
  546. aitconst_sleb128bit,
  547. aitconst_128bit:
  548. begin
  549. end;
  550. aitconst_32bit,
  551. aitconst_16bit,
  552. aitconst_8bit,
  553. aitconst_rva_symbol,
  554. aitconst_secrel32_symbol :
  555. begin
  556. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  557. l:=0;
  558. repeat
  559. if assigned(tai_const(hp).sym) then
  560. begin
  561. if SmartAsm then
  562. begin
  563. AddSymbol(tai_const(hp).sym.name,false);
  564. if assigned(tai_const(hp).endsym) then
  565. AddSymbol(tai_const(hp).endsym.name,false);
  566. end;
  567. if assigned(tai_const(hp).endsym) then
  568. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  569. else
  570. s:=tai_const(hp).sym.name;
  571. if tai_const(hp).value<>0 then
  572. s:=s+tostr_with_plus(tai_const(hp).value);
  573. end
  574. else
  575. s:=tostr(tai_const(hp).value);
  576. AsmWrite(s);
  577. inc(l,length(s));
  578. if (l>line_length) or
  579. (hp.next=nil) or
  580. (tai(hp.next).typ<>ait_const) or
  581. (tai_const(hp.next).consttype<>consttype) then
  582. break;
  583. hp:=tai(hp.next);
  584. AsmWrite(',');
  585. until false;
  586. AsmLn;
  587. end;
  588. else
  589. internalerror(200704252);
  590. end;
  591. end;
  592. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  593. ait_real_80bit :
  594. begin
  595. if do_line then
  596. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  597. { Make sure e is a extended type, bestreal could be
  598. a different type (bestreal) !! (PFV) }
  599. e:=tai_real_80bit(hp).value;
  600. AsmWrite(#9#9'DB'#9);
  601. for i:=0 to 9 do
  602. begin
  603. if i<>0 then
  604. AsmWrite(',');
  605. AsmWrite(tostr(t80bitarray(e)[i]));
  606. end;
  607. for i:=11 to tai_real_80bit(hp).savesize do
  608. AsmWrite(',0');
  609. AsmLn;
  610. end;
  611. {$else cpuextended}
  612. ait_real_80bit :
  613. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  614. {$endif cpuextended}
  615. // ait_real_64bit :
  616. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  617. ait_real_64bit :
  618. begin
  619. if do_line then
  620. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  621. d:=tai_real_64bit(hp).value;
  622. { swap the values to correct endian if required }
  623. if source_info.endian <> target_info.endian then
  624. swap64bitarray(t64bitarray(d));
  625. AsmWrite(#9#9'DB'#9);
  626. {$ifdef arm}
  627. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  628. begin
  629. for i:=4 to 7 do
  630. begin
  631. if i<>4 then
  632. AsmWrite(',');
  633. AsmWrite(tostr(t64bitarray(d)[i]));
  634. end;
  635. for i:=0 to 3 do
  636. begin
  637. AsmWrite(',');
  638. AsmWrite(tostr(t64bitarray(d)[i]));
  639. end;
  640. end
  641. else
  642. {$endif arm}
  643. begin
  644. for i:=0 to 7 do
  645. begin
  646. if i<>0 then
  647. AsmWrite(',');
  648. AsmWrite(tostr(t64bitarray(d)[i]));
  649. end;
  650. end;
  651. AsmLn;
  652. end;
  653. // ait_real_32bit :
  654. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  655. ait_real_32bit :
  656. begin
  657. if do_line then
  658. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  659. sin:=tai_real_32bit(hp).value;
  660. { swap the values to correct endian if required }
  661. if source_info.endian <> target_info.endian then
  662. swap32bitarray(t32bitarray(sin));
  663. AsmWrite(#9#9'DB'#9);
  664. for i:=0 to 3 do
  665. begin
  666. if i<>0 then
  667. AsmWrite(',');
  668. AsmWrite(tostr(t32bitarray(sin)[i]));
  669. end;
  670. AsmLn;
  671. end;
  672. // ait_comp_64bit :
  673. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  674. ait_comp_64bit :
  675. begin
  676. if do_line then
  677. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  678. AsmWrite(#9#9'DB'#9);
  679. co:=comp(tai_comp_64bit(hp).value);
  680. { swap the values to correct endian if required }
  681. if source_info.endian <> target_info.endian then
  682. swap64bitarray(t64bitarray(co));
  683. for i:=0 to 7 do
  684. begin
  685. if i<>0 then
  686. AsmWrite(',');
  687. AsmWrite(tostr(t64bitarray(co)[i]));
  688. end;
  689. AsmLn;
  690. end;
  691. ait_string :
  692. begin
  693. counter := 0;
  694. lines := tai_string(hp).len div line_length;
  695. { separate lines in different parts }
  696. if tai_string(hp).len > 0 then
  697. Begin
  698. for j := 0 to lines-1 do
  699. begin
  700. AsmWrite(#9#9'DB'#9);
  701. quoted:=false;
  702. for i:=counter to counter+line_length-1 do
  703. begin
  704. { it is an ascii character. }
  705. if (ord(tai_string(hp).str[i])>31) and
  706. (ord(tai_string(hp).str[i])<128) and
  707. (tai_string(hp).str[i]<>'"') then
  708. begin
  709. if not(quoted) then
  710. begin
  711. if i>counter then
  712. AsmWrite(',');
  713. AsmWrite('"');
  714. end;
  715. AsmWrite(tai_string(hp).str[i]);
  716. quoted:=true;
  717. end { if > 31 and < 128 and ord('"') }
  718. else
  719. begin
  720. if quoted then
  721. AsmWrite('"');
  722. if i>counter then
  723. AsmWrite(',');
  724. quoted:=false;
  725. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  726. end;
  727. end; { end for i:=0 to... }
  728. if quoted then AsmWrite('"');
  729. AsmWrite(target_info.newline);
  730. inc(counter,line_length);
  731. end; { end for j:=0 ... }
  732. { do last line of lines }
  733. if counter<tai_string(hp).len then
  734. AsmWrite(#9#9'DB'#9);
  735. quoted:=false;
  736. for i:=counter to tai_string(hp).len-1 do
  737. begin
  738. { it is an ascii character. }
  739. if (ord(tai_string(hp).str[i])>31) and
  740. (ord(tai_string(hp).str[i])<128) and
  741. (tai_string(hp).str[i]<>'"') then
  742. begin
  743. if not(quoted) then
  744. begin
  745. if i>counter then
  746. AsmWrite(',');
  747. AsmWrite('"');
  748. end;
  749. AsmWrite(tai_string(hp).str[i]);
  750. quoted:=true;
  751. end { if > 31 and < 128 and " }
  752. else
  753. begin
  754. if quoted then
  755. AsmWrite('"');
  756. if i>counter then
  757. AsmWrite(',');
  758. quoted:=false;
  759. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  760. end;
  761. end; { end for i:=0 to... }
  762. if quoted then
  763. AsmWrite('"');
  764. end;
  765. AsmLn;
  766. end;
  767. ait_label :
  768. begin
  769. if tai_label(hp).labsym.is_used then
  770. AsmWriteLn(tai_label(hp).labsym.name+':');
  771. if SmartAsm then
  772. AddSymbol(tai_label(hp).labsym.name,true);
  773. end;
  774. ait_symbol :
  775. begin
  776. if tai_symbol(hp).has_value then
  777. internalerror(2009090803);
  778. if tai_symbol(hp).is_global then
  779. begin
  780. AsmWrite(#9'GLOBAL ');
  781. AsmWriteLn(tai_symbol(hp).sym.name);
  782. end;
  783. AsmWrite(tai_symbol(hp).sym.name);
  784. if SmartAsm then
  785. AddSymbol(tai_symbol(hp).sym.name,true);
  786. if assigned(hp.next) and not(tai(hp.next).typ in
  787. [ait_const,
  788. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  789. AsmWriteLn(':')
  790. end;
  791. ait_symbol_end : ;
  792. ait_instruction :
  793. begin
  794. taicpu(hp).CheckNonCommutativeOpcodes;
  795. { We need intel order, no At&t }
  796. taicpu(hp).SetOperandOrder(op_intel);
  797. s:='';
  798. if ((taicpu(hp).opcode=A_FADDP) or
  799. (taicpu(hp).opcode=A_FMULP))
  800. and (taicpu(hp).ops=0) then
  801. begin
  802. taicpu(hp).allocate_oper(2);
  803. taicpu(hp).oper[0]^.typ:=top_reg;
  804. taicpu(hp).oper[0]^.reg:=NR_ST1;
  805. taicpu(hp).oper[1]^.typ:=top_reg;
  806. taicpu(hp).oper[1]^.reg:=NR_ST;
  807. end;
  808. if taicpu(hp).opcode=A_FWAIT then
  809. AsmWriteln(#9#9'DB'#9'09bh')
  810. else
  811. begin
  812. { We need to explicitely set
  813. word prefix to get selectors
  814. to be pushed in 2 bytes PM }
  815. if (taicpu(hp).opsize=S_W) and
  816. ((taicpu(hp).opcode=A_PUSH) or
  817. (taicpu(hp).opcode=A_POP)) and
  818. (taicpu(hp).oper[0]^.typ=top_reg) and
  819. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  820. AsmWriteln(#9#9'DB'#9'066h');
  821. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  822. if taicpu(hp).ops<>0 then
  823. begin
  824. if is_calljmp(taicpu(hp).opcode) then
  825. begin
  826. AsmWrite(#9);
  827. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  828. end
  829. else
  830. begin
  831. for i:=0 to taicpu(hp).ops-1 do
  832. begin
  833. if i=0 then
  834. AsmWrite(#9)
  835. else
  836. AsmWrite(',');
  837. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  838. end;
  839. end;
  840. end;
  841. AsmLn;
  842. end;
  843. end;
  844. ait_stab,
  845. ait_force_line,
  846. ait_function_name : ;
  847. ait_cutobject :
  848. begin
  849. if SmartAsm then
  850. begin
  851. { only reset buffer if nothing has changed }
  852. if AsmSize=AsmStartSize then
  853. AsmClear
  854. else
  855. begin
  856. if SmartAsm then
  857. begin
  858. WriteSmartExternals;
  859. FreeExternChainList;
  860. end;
  861. AsmClose;
  862. DoAssemble;
  863. AsmCreate(tai_cutobject(hp).place);
  864. end;
  865. { avoid empty files }
  866. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  867. begin
  868. if tai(hp.next).typ=ait_section then
  869. lasTSectype:=tai_section(hp.next).sectype;
  870. hp:=tai(hp.next);
  871. end;
  872. if lasTSectype<>sec_none then
  873. WriteSection(lasTSectype,'');
  874. AsmStartSize:=AsmSize;
  875. end;
  876. end;
  877. ait_marker :
  878. if tai_marker(hp).kind=mark_NoLineInfoStart then
  879. inc(InlineLevel)
  880. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  881. dec(InlineLevel);
  882. ait_directive :
  883. begin
  884. case tai_directive(hp).directive of
  885. asd_nasm_import :
  886. AsmWrite('import ');
  887. asd_extern :
  888. AsmWrite('EXTERN ');
  889. else
  890. internalerror(200509191);
  891. end;
  892. if assigned(tai_directive(hp).name) then
  893. begin
  894. if SmartAsm then
  895. AddSymbol(tai_directive(hp).name^,false);
  896. AsmWrite(tai_directive(hp).name^);
  897. end;
  898. AsmLn;
  899. end;
  900. else
  901. internalerror(10000);
  902. end;
  903. hp:=tai(hp.next);
  904. end;
  905. end;
  906. procedure T386NasmAssembler.WriteExternals;
  907. var
  908. sym : TAsmSymbol;
  909. i : longint;
  910. begin
  911. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  912. begin
  913. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  914. if sym.bind=AB_EXTERNAL then
  915. AsmWriteln('EXTERN'#9+sym.name);
  916. end;
  917. end;
  918. procedure T386NasmAssembler.WriteSmartExternals;
  919. var
  920. EC : PExternChain;
  921. begin
  922. EC:=FEC;
  923. while assigned(EC) do
  924. begin
  925. if not EC^.is_defined then
  926. AsmWriteln('EXTERN'#9+EC^.psym^);
  927. EC:=EC^.next;
  928. end;
  929. end;
  930. procedure T386NasmAssembler.WriteAsmList;
  931. var
  932. hal : tasmlisttype;
  933. begin
  934. {$ifdef EXTDEBUG}
  935. if assigned(current_module.mainsource) then
  936. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  937. {$endif}
  938. AsmWriteLn('BITS 32');
  939. AsmLn;
  940. WriteExternals;
  941. for hal:=low(TasmlistType) to high(TasmlistType) do
  942. begin
  943. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  944. writetree(current_asmdata.asmlists[hal]);
  945. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  946. end;
  947. AsmLn;
  948. if SmartAsm then
  949. begin
  950. WriteSmartExternals;
  951. FreeExternChainList;
  952. end;
  953. {$ifdef EXTDEBUG}
  954. if assigned(current_module.mainsource) then
  955. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  956. {$endif EXTDEBUG}
  957. end;
  958. {*****************************************************************************
  959. Initialize
  960. *****************************************************************************}
  961. const
  962. as_i386_nasmcoff_info : tasminfo =
  963. (
  964. id : as_i386_nasmcoff;
  965. idtxt : 'NASMCOFF';
  966. asmbin : 'nasm';
  967. asmcmd : '-f coff -o $OBJ $ASM';
  968. supported_targets : [system_i386_go32v2];
  969. flags : [af_allowdirect,af_needar,af_no_debug];
  970. labelprefix : '..@';
  971. comment : '; ';
  972. );
  973. as_i386_nasmwin32_info : tasminfo =
  974. (
  975. id : as_i386_nasmwin32;
  976. idtxt : 'NASMWIN32';
  977. asmbin : 'nasm';
  978. asmcmd : '-f win32 -o $OBJ $ASM';
  979. supported_targets : [system_i386_win32];
  980. flags : [af_allowdirect,af_needar,af_no_debug];
  981. labelprefix : '..@';
  982. comment : '; ';
  983. );
  984. as_i386_nasmobj_info : tasminfo =
  985. (
  986. id : as_i386_nasmobj;
  987. idtxt : 'NASMOBJ';
  988. asmbin : 'nasm';
  989. asmcmd : '-f obj -o $OBJ $ASM';
  990. supported_targets : [system_i386_embedded];
  991. flags : [af_allowdirect,af_needar,af_no_debug];
  992. labelprefix : '..@';
  993. comment : '; ';
  994. );
  995. as_i386_nasmwdosx_info : tasminfo =
  996. (
  997. id : as_i386_nasmwdosx;
  998. idtxt : 'NASMWDOSX';
  999. asmbin : 'nasm';
  1000. asmcmd : '-f win32 -o $OBJ $ASM';
  1001. supported_targets : [system_i386_wdosx];
  1002. flags : [af_allowdirect,af_needar,af_no_debug];
  1003. labelprefix : '..@';
  1004. comment : '; ';
  1005. );
  1006. as_i386_nasmelf_info : tasminfo =
  1007. (
  1008. id : as_i386_nasmelf;
  1009. idtxt : 'NASMELF';
  1010. asmbin : 'nasm';
  1011. asmcmd : '-f elf -o $OBJ $ASM';
  1012. supported_targets : [system_i386_linux];
  1013. flags : [af_allowdirect,af_needar,af_no_debug];
  1014. labelprefix : '..@';
  1015. comment : '; ';
  1016. );
  1017. as_i386_nasmbeos_info : tasminfo =
  1018. (
  1019. id : as_i386_nasmbeos;
  1020. idtxt : 'NASMELF';
  1021. asmbin : 'nasm';
  1022. asmcmd : '-f elf -o $OBJ $ASM';
  1023. supported_targets : [system_i386_beos];
  1024. flags : [af_allowdirect,af_needar,af_no_debug];
  1025. labelprefix : '..@';
  1026. comment : '; ';
  1027. );
  1028. as_i386_nasmhaiku_info : tasminfo =
  1029. (
  1030. id : as_i386_nasmhaiku;
  1031. idtxt : 'NASMELF';
  1032. asmbin : 'nasm';
  1033. asmcmd : '-f elf -o $OBJ $ASM';
  1034. supported_targets : [system_i386_haiku];
  1035. flags : [af_allowdirect,af_needar,af_no_debug];
  1036. labelprefix : '..@';
  1037. comment : '; ';
  1038. );
  1039. initialization
  1040. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1041. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1042. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1043. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1044. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1045. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1046. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1047. end.