ag386nsm.pas 37 KB

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