ag386nsm.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 ag386nsm;
  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. end;
  36. implementation
  37. uses
  38. cutils,globtype,globals,systems,cclasses,
  39. fmodule,finput,verbose,cpuinfo,cgbase
  40. ;
  41. type
  42. {$ifdef cpuextended}
  43. t80bitarray = array[0..9] of byte;
  44. {$endif cpuextended}
  45. t64bitarray = array[0..7] of byte;
  46. t32bitarray = array[0..3] of byte;
  47. const
  48. line_length = 64;
  49. nasm_regname_table : array[tregisterindex] of string[7] = (
  50. {r386nasm.inc contains the Nasm name of each register.}
  51. {$i r386nasm.inc}
  52. );
  53. var
  54. lastfileinfo : tfileposinfo;
  55. infile,
  56. lastinfile : tinputfile;
  57. function nasm_regname(r:Tregister):string;
  58. var
  59. p : tregisterindex;
  60. begin
  61. p:=findreg_by_number(r);
  62. if p<>0 then
  63. result:=nasm_regname_table[p]
  64. else
  65. result:=generic_regname(r);
  66. end;
  67. function fixline(s:string):string;
  68. {
  69. return s with all leading and ending spaces and tabs removed
  70. }
  71. var
  72. i,j,k : longint;
  73. begin
  74. i:=length(s);
  75. while (i>0) and (s[i] in [#9,' ']) do
  76. dec(i);
  77. j:=1;
  78. while (j<i) and (s[j] in [#9,' ']) do
  79. inc(j);
  80. for k:=j to i do
  81. if s[k] in [#0..#31,#127..#255] then
  82. s[k]:='.';
  83. fixline:=Copy(s,j,i-j+1);
  84. end;
  85. function single2str(d : single) : string;
  86. var
  87. hs : string;
  88. p : longint;
  89. begin
  90. str(d,hs);
  91. { nasm expects a lowercase e }
  92. p:=pos('E',hs);
  93. if p>0 then
  94. hs[p]:='e';
  95. p:=pos('+',hs);
  96. if p>0 then
  97. delete(hs,p,1);
  98. single2str:=lower(hs);
  99. end;
  100. function double2str(d : double) : string;
  101. var
  102. hs : string;
  103. p : longint;
  104. begin
  105. str(d,hs);
  106. { nasm expects a lowercase e }
  107. p:=pos('E',hs);
  108. if p>0 then
  109. hs[p]:='e';
  110. p:=pos('+',hs);
  111. if p>0 then
  112. delete(hs,p,1);
  113. double2str:=lower(hs);
  114. end;
  115. function extended2str(e : extended) : string;
  116. var
  117. hs : string;
  118. p : longint;
  119. begin
  120. str(e,hs);
  121. { nasm expects a lowercase e }
  122. p:=pos('E',hs);
  123. if p>0 then
  124. hs[p]:='e';
  125. p:=pos('+',hs);
  126. if p>0 then
  127. delete(hs,p,1);
  128. extended2str:=lower(hs);
  129. end;
  130. { convert floating point values }
  131. { to correct endian }
  132. procedure swap64bitarray(var t: t64bitarray);
  133. var
  134. b: byte;
  135. begin
  136. b:= t[7];
  137. t[7] := t[0];
  138. t[0] := b;
  139. b := t[6];
  140. t[6] := t[1];
  141. t[1] := b;
  142. b:= t[5];
  143. t[5] := t[2];
  144. t[2] := b;
  145. b:= t[4];
  146. t[4] := t[3];
  147. t[3] := b;
  148. end;
  149. procedure swap32bitarray(var t: t32bitarray);
  150. var
  151. b: byte;
  152. begin
  153. b:= t[1];
  154. t[1]:= t[2];
  155. t[2]:= b;
  156. b:= t[0];
  157. t[0]:= t[3];
  158. t[3]:= b;
  159. end;
  160. function comp2str(d : bestreal) : string;
  161. type
  162. pdouble = ^double;
  163. var
  164. c : comp;
  165. dd : pdouble;
  166. begin
  167. c:=comp(d);
  168. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  169. comp2str:=double2str(dd^);
  170. end;
  171. function sizestr(s:topsize;dest:boolean):string;
  172. begin
  173. case s of
  174. S_B : sizestr:='byte ';
  175. S_W : sizestr:='word ';
  176. S_L : sizestr:='dword ';
  177. S_Q : sizestr:='qword ';
  178. S_IS : sizestr:='word ';
  179. S_IL : sizestr:='dword ';
  180. S_IQ : sizestr:='qword ';
  181. S_FS : sizestr:='dword ';
  182. S_FL : sizestr:='qword ';
  183. S_FX : sizestr:='tword ';
  184. S_BW : if dest then
  185. sizestr:='word '
  186. else
  187. sizestr:='byte ';
  188. S_BL : if dest then
  189. sizestr:='dword '
  190. else
  191. sizestr:='byte ';
  192. S_WL : if dest then
  193. sizestr:='dword '
  194. else
  195. sizestr:='word ';
  196. else { S_NO }
  197. sizestr:='';
  198. end;
  199. end;
  200. Function PadTabs(const p:string;addch:char):string;
  201. var
  202. s : string;
  203. i : longint;
  204. begin
  205. i:=length(p);
  206. if addch<>#0 then
  207. begin
  208. inc(i);
  209. s:=p+addch;
  210. end
  211. else
  212. s:=p;
  213. if i<8 then
  214. PadTabs:=s+#9#9
  215. else
  216. PadTabs:=s+#9;
  217. end;
  218. {****************************************************************************
  219. T386NasmAssembler
  220. ****************************************************************************}
  221. procedure T386NasmAssembler.WriteReference(var ref : treference);
  222. var
  223. first : boolean;
  224. begin
  225. with ref do
  226. begin
  227. AsmWrite('[');
  228. first:=true;
  229. if (segment<>NR_NO) then
  230. AsmWrite(nasm_regname(segment)+':');
  231. if assigned(symbol) then
  232. begin
  233. AsmWrite(symbol.name);
  234. first:=false;
  235. end;
  236. if (base<>NR_NO) then
  237. begin
  238. if not(first) then
  239. AsmWrite('+')
  240. else
  241. first:=false;
  242. AsmWrite(nasm_regname(base))
  243. end;
  244. if (index<>NR_NO) then
  245. begin
  246. if not(first) then
  247. AsmWrite('+')
  248. else
  249. first:=false;
  250. AsmWrite(nasm_regname(index));
  251. if scalefactor<>0 then
  252. AsmWrite('*'+tostr(scalefactor));
  253. end;
  254. if offset<0 then
  255. begin
  256. AsmWrite(tostr(offset));
  257. first:=false;
  258. end
  259. else if (offset>0) then
  260. begin
  261. AsmWrite('+'+tostr(offset));
  262. first:=false;
  263. end;
  264. if first then
  265. AsmWrite('0');
  266. AsmWrite(']');
  267. end;
  268. end;
  269. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  270. begin
  271. case o.typ of
  272. top_reg :
  273. AsmWrite(nasm_regname(o.reg));
  274. top_const :
  275. begin
  276. if (ops=1) and (opcode<>A_RET) then
  277. AsmWrite(sizestr(s,dest));
  278. AsmWrite(tostr(longint(o.val)));
  279. end;
  280. top_ref :
  281. begin
  282. if o.ref^.refaddr=addr_no then
  283. begin
  284. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  285. (opcode = A_LSS) or (opcode = A_LFS) or
  286. (opcode = A_LES) or (opcode = A_LDS) or
  287. //(opcode = A_SHR) or (opcode = A_SHL) or
  288. (opcode = A_SAR) or (opcode = A_SAL) or
  289. (opcode = A_OUT) or (opcode = A_IN)) then
  290. AsmWrite(sizestr(s,dest));
  291. WriteReference(o.ref^);
  292. end
  293. else
  294. begin
  295. asmwrite('dword ');
  296. if assigned(o.ref^.symbol) then
  297. begin
  298. asmwrite(o.ref^.symbol.name);
  299. if o.ref^.offset=0 then
  300. exit;
  301. end;
  302. if o.ref^.offset>0 then
  303. asmwrite('+');
  304. asmwrite(tostr(o.ref^.offset));
  305. end;
  306. end;
  307. else
  308. internalerror(10001);
  309. end;
  310. end;
  311. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  312. begin
  313. case o.typ of
  314. top_reg :
  315. AsmWrite(nasm_regname(o.reg));
  316. top_ref :
  317. if o.ref^.refaddr=addr_no then
  318. WriteReference(o.ref^)
  319. else
  320. begin
  321. if not(
  322. (op=A_JCXZ) or (op=A_JECXZ) or
  323. {$ifdef x86_64}
  324. (op=A_JRCXZ) or
  325. {$endif x86_64}
  326. (op=A_LOOP) or (op=A_LOOPE) or
  327. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  328. (op=A_LOOPZ)
  329. ) then
  330. AsmWrite('NEAR ');
  331. AsmWrite(o.ref^.symbol.name);
  332. if o.ref^.offset>0 then
  333. AsmWrite('+'+tostr(o.ref^.offset))
  334. else
  335. if o.ref^.offset<0 then
  336. AsmWrite(tostr(o.ref^.offset));
  337. end;
  338. top_const :
  339. AsmWrite(tostr(aint(o.val)));
  340. else
  341. internalerror(10001);
  342. end;
  343. end;
  344. var
  345. LastSecType : TAsmSectiontype;
  346. const
  347. ait_const2str : array[aitconst_128bit..aitconst_indirect_symbol] of string[20]=(
  348. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  349. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  350. #9'RVA'#9,#9'SECREL32'#9,#9'FIXMEINDIRECT'#9
  351. );
  352. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  353. const
  354. secnames : array[TAsmSectiontype] of string[17] = ('',
  355. '.text',
  356. '.data',
  357. '.rodata',
  358. '.bss',
  359. '.tbss',
  360. '.pdata',
  361. '.text',
  362. '.stab',
  363. '.stabstr',
  364. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  365. '.eh_frame',
  366. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  367. '.fpc',
  368. '',
  369. '.init'
  370. );
  371. begin
  372. AsmLn;
  373. AsmWrite('SECTION ');
  374. AsmWrite(secnames[atype]);
  375. if use_smartlink_section and
  376. (atype<>sec_bss) and
  377. (aname<>'') then
  378. begin
  379. AsmWrite('.');
  380. AsmWrite(aname);
  381. end;
  382. AsmLn;
  383. LasTSecType:=atype;
  384. end;
  385. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  386. {$ifdef cpuextended}
  387. type
  388. t80bitarray = array[0..9] of byte;
  389. {$endif cpuextended}
  390. var
  391. s : string;
  392. hp : tai;
  393. hp1 : tailineinfo;
  394. counter,
  395. lines,
  396. i,j,l : longint;
  397. InlineLevel : longint;
  398. consttype : taiconst_type;
  399. do_line,
  400. quoted : boolean;
  401. co : comp;
  402. sin : single;
  403. d : double;
  404. {$ifdef cpuextended}
  405. e : extended;
  406. {$endif cpuextended}
  407. begin
  408. if not assigned(p) then
  409. exit;
  410. InlineLevel:=0;
  411. { lineinfo is only needed for al_procedures (PFV) }
  412. do_line:=(cs_asm_source in current_settings.globalswitches) or
  413. ((cs_lineinfo in current_settings.moduleswitches)
  414. and (p=current_asmdata.asmlists[al_procedures]));
  415. hp:=tai(p.first);
  416. while assigned(hp) do
  417. begin
  418. if not(hp.typ in SkipLineInfo) then
  419. begin
  420. hp1:=hp as tailineinfo;
  421. current_filepos:=hp1.fileinfo;
  422. if do_line then
  423. begin
  424. { load infile }
  425. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  426. begin
  427. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  428. if assigned(infile) then
  429. begin
  430. { open only if needed !! }
  431. if (cs_asm_source in current_settings.globalswitches) then
  432. infile.open;
  433. end;
  434. { avoid unnecessary reopens of the same file !! }
  435. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  436. { be sure to change line !! }
  437. lastfileinfo.line:=-1;
  438. end;
  439. { write source }
  440. if (cs_asm_source in current_settings.globalswitches) and
  441. assigned(infile) then
  442. begin
  443. if (infile<>lastinfile) then
  444. begin
  445. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  446. if assigned(lastinfile) then
  447. lastinfile.close;
  448. end;
  449. if (hp1.fileinfo.line<>lastfileinfo.line) and
  450. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  451. begin
  452. if (hp1.fileinfo.line<>0) and
  453. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  454. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  455. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  456. { set it to a negative value !
  457. to make that is has been read already !! PM }
  458. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  459. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  460. end;
  461. end;
  462. lastfileinfo:=hp1.fileinfo;
  463. lastinfile:=infile;
  464. end;
  465. end;
  466. case hp.typ of
  467. ait_comment :
  468. Begin
  469. AsmWrite(target_asm.comment);
  470. AsmWritePChar(tai_comment(hp).str);
  471. AsmLn;
  472. End;
  473. ait_regalloc :
  474. begin
  475. if (cs_asm_regalloc in current_settings.globalswitches) then
  476. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  477. regallocstr[tai_regalloc(hp).ratype]);
  478. end;
  479. ait_tempalloc :
  480. begin
  481. if (cs_asm_tempalloc in current_settings.globalswitches) then
  482. begin
  483. {$ifdef EXTDEBUG}
  484. if assigned(tai_tempalloc(hp).problem) then
  485. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  486. tostr(tai_tempalloc(hp).tempsize)+')')
  487. else
  488. {$endif EXTDEBUG}
  489. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  490. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  491. end;
  492. end;
  493. ait_section :
  494. begin
  495. if tai_section(hp).sectype<>sec_none then
  496. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  497. LasTSecType:=tai_section(hp).sectype;
  498. end;
  499. ait_align :
  500. begin
  501. { nasm gives warnings when it finds align in bss as it
  502. wants to store data }
  503. if (lastsectype<>sec_bss) and
  504. (tai_align(hp).aligntype>1) then
  505. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  506. end;
  507. ait_datablock :
  508. begin
  509. if tai_datablock(hp).is_global then
  510. begin
  511. AsmWrite(#9'GLOBAL ');
  512. AsmWriteLn(tai_datablock(hp).sym.name);
  513. end;
  514. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  515. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  516. end;
  517. ait_const:
  518. begin
  519. consttype:=tai_const(hp).consttype;
  520. case consttype of
  521. aitconst_64bit :
  522. begin
  523. if assigned(tai_const(hp).sym) then
  524. internalerror(200404292);
  525. AsmWrite(ait_const2str[aitconst_32bit]);
  526. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  527. AsmWrite(',');
  528. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  529. AsmLn;
  530. end;
  531. aitconst_uleb128bit,
  532. aitconst_sleb128bit,
  533. aitconst_128bit:
  534. begin
  535. end;
  536. aitconst_32bit,
  537. aitconst_16bit,
  538. aitconst_8bit,
  539. aitconst_rva_symbol,
  540. aitconst_secrel32_symbol,
  541. aitconst_indirect_symbol :
  542. begin
  543. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  544. l:=0;
  545. repeat
  546. if assigned(tai_const(hp).sym) then
  547. begin
  548. if assigned(tai_const(hp).endsym) then
  549. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  550. else
  551. s:=tai_const(hp).sym.name;
  552. if tai_const(hp).value<>0 then
  553. s:=s+tostr_with_plus(tai_const(hp).value);
  554. end
  555. else
  556. s:=tostr(tai_const(hp).value);
  557. AsmWrite(s);
  558. inc(l,length(s));
  559. if (l>line_length) or
  560. (hp.next=nil) or
  561. (tai(hp.next).typ<>ait_const) or
  562. (tai_const(hp.next).consttype<>consttype) then
  563. break;
  564. hp:=tai(hp.next);
  565. AsmWrite(',');
  566. until false;
  567. AsmLn;
  568. end;
  569. else
  570. internalerror(200704252);
  571. end;
  572. end;
  573. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  574. ait_real_80bit :
  575. begin
  576. if do_line then
  577. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  578. { Make sure e is a extended type, bestreal could be
  579. a different type (bestreal) !! (PFV) }
  580. e:=tai_real_80bit(hp).value;
  581. AsmWrite(#9#9'DB'#9);
  582. for i:=0 to 9 do
  583. begin
  584. if i<>0 then
  585. AsmWrite(',');
  586. AsmWrite(tostr(t80bitarray(e)[i]));
  587. end;
  588. AsmLn;
  589. end;
  590. {$else cpuextended}
  591. ait_real_80bit :
  592. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  593. {$endif cpuextended}
  594. // ait_real_64bit :
  595. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  596. ait_real_64bit :
  597. begin
  598. if do_line then
  599. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  600. d:=tai_real_64bit(hp).value;
  601. { swap the values to correct endian if required }
  602. if source_info.endian <> target_info.endian then
  603. swap64bitarray(t64bitarray(d));
  604. AsmWrite(#9#9'DB'#9);
  605. {$ifdef arm}
  606. { on a real arm cpu, it's already hi/lo swapped }
  607. {$ifndef cpuarm}
  608. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  609. begin
  610. for i:=4 to 7 do
  611. begin
  612. if i<>4 then
  613. AsmWrite(',');
  614. AsmWrite(tostr(t64bitarray(d)[i]));
  615. end;
  616. for i:=0 to 3 do
  617. begin
  618. AsmWrite(',');
  619. AsmWrite(tostr(t64bitarray(d)[i]));
  620. end;
  621. end
  622. else
  623. {$endif cpuarm}
  624. {$endif arm}
  625. begin
  626. for i:=0 to 7 do
  627. begin
  628. if i<>0 then
  629. AsmWrite(',');
  630. AsmWrite(tostr(t64bitarray(d)[i]));
  631. end;
  632. end;
  633. AsmLn;
  634. end;
  635. // ait_real_32bit :
  636. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  637. ait_real_32bit :
  638. begin
  639. if do_line then
  640. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  641. sin:=tai_real_32bit(hp).value;
  642. { swap the values to correct endian if required }
  643. if source_info.endian <> target_info.endian then
  644. swap32bitarray(t32bitarray(sin));
  645. AsmWrite(#9#9'DB'#9);
  646. for i:=0 to 3 do
  647. begin
  648. if i<>0 then
  649. AsmWrite(',');
  650. AsmWrite(tostr(t32bitarray(sin)[i]));
  651. end;
  652. AsmLn;
  653. end;
  654. // ait_comp_64bit :
  655. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  656. ait_comp_64bit :
  657. begin
  658. if do_line then
  659. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  660. AsmWrite(#9#9'DB'#9);
  661. co:=comp(tai_comp_64bit(hp).value);
  662. { swap the values to correct endian if required }
  663. if source_info.endian <> target_info.endian then
  664. swap64bitarray(t64bitarray(co));
  665. for i:=0 to 7 do
  666. begin
  667. if i<>0 then
  668. AsmWrite(',');
  669. AsmWrite(tostr(t64bitarray(co)[i]));
  670. end;
  671. AsmLn;
  672. end;
  673. ait_string :
  674. begin
  675. counter := 0;
  676. lines := tai_string(hp).len div line_length;
  677. { separate lines in different parts }
  678. if tai_string(hp).len > 0 then
  679. Begin
  680. for j := 0 to lines-1 do
  681. begin
  682. AsmWrite(#9#9'DB'#9);
  683. quoted:=false;
  684. for i:=counter to counter+line_length-1 do
  685. begin
  686. { it is an ascii character. }
  687. if (ord(tai_string(hp).str[i])>31) and
  688. (ord(tai_string(hp).str[i])<128) and
  689. (tai_string(hp).str[i]<>'"') then
  690. begin
  691. if not(quoted) then
  692. begin
  693. if i>counter then
  694. AsmWrite(',');
  695. AsmWrite('"');
  696. end;
  697. AsmWrite(tai_string(hp).str[i]);
  698. quoted:=true;
  699. end { if > 31 and < 128 and ord('"') }
  700. else
  701. begin
  702. if quoted then
  703. AsmWrite('"');
  704. if i>counter then
  705. AsmWrite(',');
  706. quoted:=false;
  707. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  708. end;
  709. end; { end for i:=0 to... }
  710. if quoted then AsmWrite('"');
  711. AsmWrite(target_info.newline);
  712. inc(counter,line_length);
  713. end; { end for j:=0 ... }
  714. { do last line of lines }
  715. if counter<tai_string(hp).len then
  716. AsmWrite(#9#9'DB'#9);
  717. quoted:=false;
  718. for i:=counter to tai_string(hp).len-1 do
  719. begin
  720. { it is an ascii character. }
  721. if (ord(tai_string(hp).str[i])>31) and
  722. (ord(tai_string(hp).str[i])<128) and
  723. (tai_string(hp).str[i]<>'"') then
  724. begin
  725. if not(quoted) then
  726. begin
  727. if i>counter then
  728. AsmWrite(',');
  729. AsmWrite('"');
  730. end;
  731. AsmWrite(tai_string(hp).str[i]);
  732. quoted:=true;
  733. end { if > 31 and < 128 and " }
  734. else
  735. begin
  736. if quoted then
  737. AsmWrite('"');
  738. if i>counter then
  739. AsmWrite(',');
  740. quoted:=false;
  741. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  742. end;
  743. end; { end for i:=0 to... }
  744. if quoted then
  745. AsmWrite('"');
  746. end;
  747. AsmLn;
  748. end;
  749. ait_label :
  750. begin
  751. if tai_label(hp).labsym.is_used then
  752. AsmWriteLn(tai_label(hp).labsym.name+':');
  753. end;
  754. ait_symbol :
  755. begin
  756. if tai_symbol(hp).is_global then
  757. begin
  758. AsmWrite(#9'GLOBAL ');
  759. AsmWriteLn(tai_symbol(hp).sym.name);
  760. end;
  761. AsmWrite(tai_symbol(hp).sym.name);
  762. if assigned(hp.next) and not(tai(hp.next).typ in
  763. [ait_const,
  764. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  765. AsmWriteLn(':')
  766. end;
  767. ait_symbol_end : ;
  768. ait_instruction :
  769. begin
  770. taicpu(hp).CheckNonCommutativeOpcodes;
  771. { We need intel order, no At&t }
  772. taicpu(hp).SetOperandOrder(op_intel);
  773. s:='';
  774. if ((taicpu(hp).opcode=A_FADDP) or
  775. (taicpu(hp).opcode=A_FMULP))
  776. and (taicpu(hp).ops=0) then
  777. begin
  778. taicpu(hp).allocate_oper(2);
  779. taicpu(hp).oper[0]^.typ:=top_reg;
  780. taicpu(hp).oper[0]^.reg:=NR_ST1;
  781. taicpu(hp).oper[1]^.typ:=top_reg;
  782. taicpu(hp).oper[1]^.reg:=NR_ST;
  783. end;
  784. if taicpu(hp).opcode=A_FWAIT then
  785. AsmWriteln(#9#9'DB'#9'09bh')
  786. else
  787. begin
  788. { We need to explicitely set
  789. word prefix to get selectors
  790. to be pushed in 2 bytes PM }
  791. if (taicpu(hp).opsize=S_W) and
  792. ((taicpu(hp).opcode=A_PUSH) or
  793. (taicpu(hp).opcode=A_POP)) and
  794. (taicpu(hp).oper[0]^.typ=top_reg) and
  795. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  796. AsmWriteln(#9#9'DB'#9'066h');
  797. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  798. if taicpu(hp).ops<>0 then
  799. begin
  800. if is_calljmp(taicpu(hp).opcode) then
  801. begin
  802. AsmWrite(#9);
  803. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  804. end
  805. else
  806. begin
  807. for i:=0 to taicpu(hp).ops-1 do
  808. begin
  809. if i=0 then
  810. AsmWrite(#9)
  811. else
  812. AsmWrite(',');
  813. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  814. end;
  815. end;
  816. end;
  817. AsmLn;
  818. end;
  819. end;
  820. ait_stab,
  821. ait_force_line,
  822. ait_function_name : ;
  823. ait_cutobject :
  824. begin
  825. { only reset buffer if nothing has changed }
  826. if AsmSize=AsmStartSize then
  827. AsmClear
  828. else
  829. begin
  830. AsmClose;
  831. DoAssemble;
  832. AsmCreate(tai_cutobject(hp).place);
  833. end;
  834. { avoid empty files }
  835. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  836. begin
  837. if tai(hp.next).typ=ait_section then
  838. lasTSectype:=tai_section(hp.next).sectype;
  839. hp:=tai(hp.next);
  840. end;
  841. if lasTSectype<>sec_none then
  842. WriteSection(lasTSectype,'');
  843. AsmStartSize:=AsmSize;
  844. end;
  845. ait_marker :
  846. if tai_marker(hp).kind=mark_InlineStart then
  847. inc(InlineLevel)
  848. else if tai_marker(hp).kind=mark_InlineEnd then
  849. dec(InlineLevel);
  850. ait_directive :
  851. begin
  852. case tai_directive(hp).directive of
  853. asd_nasm_import :
  854. AsmWrite('import ');
  855. asd_extern :
  856. AsmWrite('EXTERN ');
  857. else
  858. internalerror(200509191);
  859. end;
  860. if assigned(tai_directive(hp).name) then
  861. AsmWrite(tai_directive(hp).name^);
  862. AsmLn;
  863. end;
  864. else
  865. internalerror(10000);
  866. end;
  867. hp:=tai(hp.next);
  868. end;
  869. end;
  870. procedure T386NasmAssembler.WriteExternals;
  871. var
  872. sym : TAsmSymbol;
  873. i : longint;
  874. begin
  875. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  876. begin
  877. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  878. if sym.bind=AB_EXTERNAL then
  879. AsmWriteln('EXTERN'#9+sym.name);
  880. end;
  881. end;
  882. procedure T386NasmAssembler.WriteAsmList;
  883. var
  884. hal : tasmlisttype;
  885. begin
  886. {$ifdef EXTDEBUG}
  887. if assigned(current_module.mainsource) then
  888. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  889. {$endif}
  890. LasTSecType:=sec_none;
  891. AsmWriteLn('BITS 32');
  892. AsmLn;
  893. lastfileinfo.line:=-1;
  894. lastfileinfo.fileindex:=0;
  895. lastinfile:=nil;
  896. WriteExternals;
  897. for hal:=low(TasmlistType) to high(TasmlistType) do
  898. begin
  899. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  900. writetree(current_asmdata.asmlists[hal]);
  901. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  902. end;
  903. AsmLn;
  904. {$ifdef EXTDEBUG}
  905. if assigned(current_module.mainsource) then
  906. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  907. {$endif EXTDEBUG}
  908. end;
  909. {*****************************************************************************
  910. Initialize
  911. *****************************************************************************}
  912. const
  913. as_i386_nasmcoff_info : tasminfo =
  914. (
  915. id : as_i386_nasmcoff;
  916. idtxt : 'NASMCOFF';
  917. asmbin : 'nasm';
  918. asmcmd : '-f coff -o $OBJ $ASM';
  919. supported_target : system_i386_go32v2;
  920. flags : [af_allowdirect,af_needar,af_no_debug];
  921. labelprefix : '..@';
  922. comment : '; ';
  923. );
  924. as_i386_nasmwin32_info : tasminfo =
  925. (
  926. id : as_i386_nasmwin32;
  927. idtxt : 'NASMWIN32';
  928. asmbin : 'nasm';
  929. asmcmd : '-f win32 -o $OBJ $ASM';
  930. supported_target : system_i386_win32;
  931. flags : [af_allowdirect,af_needar,af_no_debug];
  932. labelprefix : '..@';
  933. comment : '; ';
  934. );
  935. as_i386_nasmobj_info : tasminfo =
  936. (
  937. id : as_i386_nasmobj;
  938. idtxt : 'NASMOBJ';
  939. asmbin : 'nasm';
  940. asmcmd : '-f obj -o $OBJ $ASM';
  941. supported_target : system_any; { what should I write here ?? }
  942. flags : [af_allowdirect,af_needar,af_no_debug];
  943. labelprefix : '..@';
  944. comment : '; ';
  945. );
  946. as_i386_nasmwdosx_info : tasminfo =
  947. (
  948. id : as_i386_nasmwdosx;
  949. idtxt : 'NASMWDOSX';
  950. asmbin : 'nasm';
  951. asmcmd : '-f win32 -o $OBJ $ASM';
  952. supported_target : system_i386_wdosx;
  953. flags : [af_allowdirect,af_needar,af_no_debug];
  954. labelprefix : '..@';
  955. comment : '; ';
  956. );
  957. as_i386_nasmelf_info : tasminfo =
  958. (
  959. id : as_i386_nasmelf;
  960. idtxt : 'NASMELF';
  961. asmbin : 'nasm';
  962. asmcmd : '-f elf -o $OBJ $ASM';
  963. supported_target : system_i386_linux;
  964. flags : [af_allowdirect,af_needar,af_no_debug];
  965. labelprefix : '..@';
  966. comment : '; ';
  967. );
  968. as_i386_nasmbeos_info : tasminfo =
  969. (
  970. id : as_i386_nasmbeos;
  971. idtxt : 'NASMELF';
  972. asmbin : 'nasm';
  973. asmcmd : '-f elf -o $OBJ $ASM';
  974. supported_target : system_i386_beos;
  975. flags : [af_allowdirect,af_needar,af_no_debug];
  976. labelprefix : '..@';
  977. comment : '; ';
  978. );
  979. initialization
  980. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  981. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  982. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  983. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  984. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  985. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  986. end.