2
0

agx86nsm.pas 37 KB

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