agx86nsm.pas 38 KB

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