agx86nsm.pas 36 KB

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