agx86nsm.pas 38 KB

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