ag386nsm.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  607. begin
  608. for i:=4 to 7 do
  609. begin
  610. if i<>4 then
  611. AsmWrite(',');
  612. AsmWrite(tostr(t64bitarray(d)[i]));
  613. end;
  614. for i:=0 to 3 do
  615. begin
  616. AsmWrite(',');
  617. AsmWrite(tostr(t64bitarray(d)[i]));
  618. end;
  619. end
  620. else
  621. {$endif arm}
  622. begin
  623. for i:=0 to 7 do
  624. begin
  625. if i<>0 then
  626. AsmWrite(',');
  627. AsmWrite(tostr(t64bitarray(d)[i]));
  628. end;
  629. end;
  630. AsmLn;
  631. end;
  632. // ait_real_32bit :
  633. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  634. ait_real_32bit :
  635. begin
  636. if do_line then
  637. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  638. sin:=tai_real_32bit(hp).value;
  639. { swap the values to correct endian if required }
  640. if source_info.endian <> target_info.endian then
  641. swap32bitarray(t32bitarray(sin));
  642. AsmWrite(#9#9'DB'#9);
  643. for i:=0 to 3 do
  644. begin
  645. if i<>0 then
  646. AsmWrite(',');
  647. AsmWrite(tostr(t32bitarray(sin)[i]));
  648. end;
  649. AsmLn;
  650. end;
  651. // ait_comp_64bit :
  652. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  653. ait_comp_64bit :
  654. begin
  655. if do_line then
  656. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  657. AsmWrite(#9#9'DB'#9);
  658. co:=comp(tai_comp_64bit(hp).value);
  659. { swap the values to correct endian if required }
  660. if source_info.endian <> target_info.endian then
  661. swap64bitarray(t64bitarray(co));
  662. for i:=0 to 7 do
  663. begin
  664. if i<>0 then
  665. AsmWrite(',');
  666. AsmWrite(tostr(t64bitarray(co)[i]));
  667. end;
  668. AsmLn;
  669. end;
  670. ait_string :
  671. begin
  672. counter := 0;
  673. lines := tai_string(hp).len div line_length;
  674. { separate lines in different parts }
  675. if tai_string(hp).len > 0 then
  676. Begin
  677. for j := 0 to lines-1 do
  678. begin
  679. AsmWrite(#9#9'DB'#9);
  680. quoted:=false;
  681. for i:=counter to counter+line_length-1 do
  682. begin
  683. { it is an ascii character. }
  684. if (ord(tai_string(hp).str[i])>31) and
  685. (ord(tai_string(hp).str[i])<128) and
  686. (tai_string(hp).str[i]<>'"') then
  687. begin
  688. if not(quoted) then
  689. begin
  690. if i>counter then
  691. AsmWrite(',');
  692. AsmWrite('"');
  693. end;
  694. AsmWrite(tai_string(hp).str[i]);
  695. quoted:=true;
  696. end { if > 31 and < 128 and ord('"') }
  697. else
  698. begin
  699. if quoted then
  700. AsmWrite('"');
  701. if i>counter then
  702. AsmWrite(',');
  703. quoted:=false;
  704. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  705. end;
  706. end; { end for i:=0 to... }
  707. if quoted then AsmWrite('"');
  708. AsmWrite(target_info.newline);
  709. inc(counter,line_length);
  710. end; { end for j:=0 ... }
  711. { do last line of lines }
  712. if counter<tai_string(hp).len then
  713. AsmWrite(#9#9'DB'#9);
  714. quoted:=false;
  715. for i:=counter to tai_string(hp).len-1 do
  716. begin
  717. { it is an ascii character. }
  718. if (ord(tai_string(hp).str[i])>31) and
  719. (ord(tai_string(hp).str[i])<128) and
  720. (tai_string(hp).str[i]<>'"') then
  721. begin
  722. if not(quoted) then
  723. begin
  724. if i>counter then
  725. AsmWrite(',');
  726. AsmWrite('"');
  727. end;
  728. AsmWrite(tai_string(hp).str[i]);
  729. quoted:=true;
  730. end { if > 31 and < 128 and " }
  731. else
  732. begin
  733. if quoted then
  734. AsmWrite('"');
  735. if i>counter then
  736. AsmWrite(',');
  737. quoted:=false;
  738. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  739. end;
  740. end; { end for i:=0 to... }
  741. if quoted then
  742. AsmWrite('"');
  743. end;
  744. AsmLn;
  745. end;
  746. ait_label :
  747. begin
  748. if tai_label(hp).labsym.is_used then
  749. AsmWriteLn(tai_label(hp).labsym.name+':');
  750. end;
  751. ait_symbol :
  752. begin
  753. if tai_symbol(hp).is_global then
  754. begin
  755. AsmWrite(#9'GLOBAL ');
  756. AsmWriteLn(tai_symbol(hp).sym.name);
  757. end;
  758. AsmWrite(tai_symbol(hp).sym.name);
  759. if assigned(hp.next) and not(tai(hp.next).typ in
  760. [ait_const,
  761. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  762. AsmWriteLn(':')
  763. end;
  764. ait_symbol_end : ;
  765. ait_instruction :
  766. begin
  767. taicpu(hp).CheckNonCommutativeOpcodes;
  768. { We need intel order, no At&t }
  769. taicpu(hp).SetOperandOrder(op_intel);
  770. s:='';
  771. if ((taicpu(hp).opcode=A_FADDP) or
  772. (taicpu(hp).opcode=A_FMULP))
  773. and (taicpu(hp).ops=0) then
  774. begin
  775. taicpu(hp).allocate_oper(2);
  776. taicpu(hp).oper[0]^.typ:=top_reg;
  777. taicpu(hp).oper[0]^.reg:=NR_ST1;
  778. taicpu(hp).oper[1]^.typ:=top_reg;
  779. taicpu(hp).oper[1]^.reg:=NR_ST;
  780. end;
  781. if taicpu(hp).opcode=A_FWAIT then
  782. AsmWriteln(#9#9'DB'#9'09bh')
  783. else
  784. begin
  785. { We need to explicitely set
  786. word prefix to get selectors
  787. to be pushed in 2 bytes PM }
  788. if (taicpu(hp).opsize=S_W) and
  789. ((taicpu(hp).opcode=A_PUSH) or
  790. (taicpu(hp).opcode=A_POP)) and
  791. (taicpu(hp).oper[0]^.typ=top_reg) and
  792. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  793. AsmWriteln(#9#9'DB'#9'066h');
  794. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  795. if taicpu(hp).ops<>0 then
  796. begin
  797. if is_calljmp(taicpu(hp).opcode) then
  798. begin
  799. AsmWrite(#9);
  800. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  801. end
  802. else
  803. begin
  804. for i:=0 to taicpu(hp).ops-1 do
  805. begin
  806. if i=0 then
  807. AsmWrite(#9)
  808. else
  809. AsmWrite(',');
  810. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  811. end;
  812. end;
  813. end;
  814. AsmLn;
  815. end;
  816. end;
  817. ait_stab,
  818. ait_force_line,
  819. ait_function_name : ;
  820. ait_cutobject :
  821. begin
  822. { only reset buffer if nothing has changed }
  823. if AsmSize=AsmStartSize then
  824. AsmClear
  825. else
  826. begin
  827. AsmClose;
  828. DoAssemble;
  829. AsmCreate(tai_cutobject(hp).place);
  830. end;
  831. { avoid empty files }
  832. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  833. begin
  834. if tai(hp.next).typ=ait_section then
  835. lasTSectype:=tai_section(hp.next).sectype;
  836. hp:=tai(hp.next);
  837. end;
  838. if lasTSectype<>sec_none then
  839. WriteSection(lasTSectype,'');
  840. AsmStartSize:=AsmSize;
  841. end;
  842. ait_marker :
  843. if tai_marker(hp).kind=mark_InlineStart then
  844. inc(InlineLevel)
  845. else if tai_marker(hp).kind=mark_InlineEnd then
  846. dec(InlineLevel);
  847. ait_directive :
  848. begin
  849. case tai_directive(hp).directive of
  850. asd_nasm_import :
  851. AsmWrite('import ');
  852. asd_extern :
  853. AsmWrite('EXTERN ');
  854. else
  855. internalerror(200509191);
  856. end;
  857. if assigned(tai_directive(hp).name) then
  858. AsmWrite(tai_directive(hp).name^);
  859. AsmLn;
  860. end;
  861. else
  862. internalerror(10000);
  863. end;
  864. hp:=tai(hp.next);
  865. end;
  866. end;
  867. procedure T386NasmAssembler.WriteExternals;
  868. var
  869. sym : TAsmSymbol;
  870. i : longint;
  871. begin
  872. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  873. begin
  874. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  875. if sym.bind=AB_EXTERNAL then
  876. AsmWriteln('EXTERN'#9+sym.name);
  877. end;
  878. end;
  879. procedure T386NasmAssembler.WriteAsmList;
  880. var
  881. hal : tasmlisttype;
  882. begin
  883. {$ifdef EXTDEBUG}
  884. if assigned(current_module.mainsource) then
  885. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  886. {$endif}
  887. LasTSecType:=sec_none;
  888. AsmWriteLn('BITS 32');
  889. AsmLn;
  890. lastfileinfo.line:=-1;
  891. lastfileinfo.fileindex:=0;
  892. lastinfile:=nil;
  893. WriteExternals;
  894. for hal:=low(TasmlistType) to high(TasmlistType) do
  895. begin
  896. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  897. writetree(current_asmdata.asmlists[hal]);
  898. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  899. end;
  900. AsmLn;
  901. {$ifdef EXTDEBUG}
  902. if assigned(current_module.mainsource) then
  903. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  904. {$endif EXTDEBUG}
  905. end;
  906. {*****************************************************************************
  907. Initialize
  908. *****************************************************************************}
  909. const
  910. as_i386_nasmcoff_info : tasminfo =
  911. (
  912. id : as_i386_nasmcoff;
  913. idtxt : 'NASMCOFF';
  914. asmbin : 'nasm';
  915. asmcmd : '-f coff -o $OBJ $ASM';
  916. supported_target : system_i386_go32v2;
  917. flags : [af_allowdirect,af_needar,af_no_debug];
  918. labelprefix : '..@';
  919. comment : '; ';
  920. );
  921. as_i386_nasmwin32_info : tasminfo =
  922. (
  923. id : as_i386_nasmwin32;
  924. idtxt : 'NASMWIN32';
  925. asmbin : 'nasm';
  926. asmcmd : '-f win32 -o $OBJ $ASM';
  927. supported_target : system_i386_win32;
  928. flags : [af_allowdirect,af_needar,af_no_debug];
  929. labelprefix : '..@';
  930. comment : '; ';
  931. );
  932. as_i386_nasmobj_info : tasminfo =
  933. (
  934. id : as_i386_nasmobj;
  935. idtxt : 'NASMOBJ';
  936. asmbin : 'nasm';
  937. asmcmd : '-f obj -o $OBJ $ASM';
  938. supported_target : system_any; { what should I write here ?? }
  939. flags : [af_allowdirect,af_needar,af_no_debug];
  940. labelprefix : '..@';
  941. comment : '; ';
  942. );
  943. as_i386_nasmwdosx_info : tasminfo =
  944. (
  945. id : as_i386_nasmwdosx;
  946. idtxt : 'NASMWDOSX';
  947. asmbin : 'nasm';
  948. asmcmd : '-f win32 -o $OBJ $ASM';
  949. supported_target : system_i386_wdosx;
  950. flags : [af_allowdirect,af_needar,af_no_debug];
  951. labelprefix : '..@';
  952. comment : '; ';
  953. );
  954. as_i386_nasmelf_info : tasminfo =
  955. (
  956. id : as_i386_nasmelf;
  957. idtxt : 'NASMELF';
  958. asmbin : 'nasm';
  959. asmcmd : '-f elf -o $OBJ $ASM';
  960. supported_target : system_i386_linux;
  961. flags : [af_allowdirect,af_needar,af_no_debug];
  962. labelprefix : '..@';
  963. comment : '; ';
  964. );
  965. as_i386_nasmbeos_info : tasminfo =
  966. (
  967. id : as_i386_nasmbeos;
  968. idtxt : 'NASMELF';
  969. asmbin : 'nasm';
  970. asmcmd : '-f elf -o $OBJ $ASM';
  971. supported_target : system_i386_beos;
  972. flags : [af_allowdirect,af_needar,af_no_debug];
  973. labelprefix : '..@';
  974. comment : '; ';
  975. );
  976. initialization
  977. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  978. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  979. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  980. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  981. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  982. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  983. end.