ag386nsm.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asmoutput class for the Nasm assembler with
  5. Intel syntax for the i386+
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ag386nsm;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. cpubase,
  24. aasmbase,aasmtai,aasmcpu,assemble;
  25. type
  26. T386NasmAssembler = class(texternalassembler)
  27. private
  28. procedure WriteReference(var ref : treference);
  29. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  30. procedure WriteOper_jmp(const o:toper; op : tasmop);
  31. public
  32. procedure WriteTree(p:taasmoutput);override;
  33. procedure WriteAsmList;override;
  34. procedure WriteExternals;
  35. end;
  36. implementation
  37. uses
  38. {$ifdef delphi}
  39. sysutils,
  40. {$endif}
  41. cutils,globtype,globals,systems,cclasses,
  42. fmodule,finput,verbose,cpuinfo,ag386int
  43. ;
  44. const
  45. line_length = 64;
  46. int_nasmreg2str : reg2strtable = ('',
  47. 'eax','ecx','edx','ebx','esp','ebp','esi','edi',
  48. 'ax','cx','dx','bx','sp','bp','si','di',
  49. 'al','cl','dl','bl','ah','ch','bh','dh',
  50. 'cs','ds','es','ss','fs','gs',
  51. 'st0','st0','st1','st2','st3','st4','st5','st6','st7',
  52. 'dr0','dr1','dr2','dr3','dr6','dr7',
  53. 'cr0','cr2','cr3','cr4',
  54. 'tr3','tr4','tr5','tr6','tr7',
  55. 'mm0','mm1','mm2','mm3','mm4','mm5','mm6','mm7',
  56. 'xmm0','xmm1','xmm2','xmm3','xmm4','xmm5','xmm6','xmm7'
  57. );
  58. var
  59. lastfileinfo : tfileposinfo;
  60. infile,
  61. lastinfile : tinputfile;
  62. function fixline(s:string):string;
  63. {
  64. return s with all leading and ending spaces and tabs removed
  65. }
  66. var
  67. i,j,k : longint;
  68. begin
  69. i:=length(s);
  70. while (i>0) and (s[i] in [#9,' ']) do
  71. dec(i);
  72. j:=1;
  73. while (j<i) and (s[j] in [#9,' ']) do
  74. inc(j);
  75. for k:=j to i do
  76. if s[k] in [#0..#31,#127..#255] then
  77. s[k]:='.';
  78. fixline:=Copy(s,j,i-j+1);
  79. end;
  80. function single2str(d : single) : string;
  81. var
  82. hs : string;
  83. p : byte;
  84. begin
  85. str(d,hs);
  86. { nasm expects a lowercase e }
  87. p:=pos('E',hs);
  88. if p>0 then
  89. hs[p]:='e';
  90. p:=pos('+',hs);
  91. if p>0 then
  92. delete(hs,p,1);
  93. single2str:=lower(hs);
  94. end;
  95. function double2str(d : double) : string;
  96. var
  97. hs : string;
  98. p : byte;
  99. begin
  100. str(d,hs);
  101. { nasm expects a lowercase e }
  102. p:=pos('E',hs);
  103. if p>0 then
  104. hs[p]:='e';
  105. p:=pos('+',hs);
  106. if p>0 then
  107. delete(hs,p,1);
  108. double2str:=lower(hs);
  109. end;
  110. function extended2str(e : extended) : string;
  111. var
  112. hs : string;
  113. p : byte;
  114. begin
  115. str(e,hs);
  116. { nasm expects a lowercase e }
  117. p:=pos('E',hs);
  118. if p>0 then
  119. hs[p]:='e';
  120. p:=pos('+',hs);
  121. if p>0 then
  122. delete(hs,p,1);
  123. extended2str:=lower(hs);
  124. end;
  125. function comp2str(d : bestreal) : string;
  126. type
  127. pdouble = ^double;
  128. var
  129. c : comp;
  130. dd : pdouble;
  131. begin
  132. {$ifdef FPC}
  133. c:=comp(d);
  134. {$else}
  135. c:=d;
  136. {$endif}
  137. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  138. comp2str:=double2str(dd^);
  139. end;
  140. function sizestr(s:topsize;dest:boolean):string;
  141. begin
  142. case s of
  143. S_B : sizestr:='byte ';
  144. S_W : sizestr:='word ';
  145. S_L : sizestr:='dword ';
  146. S_IS : sizestr:='word ';
  147. S_IL : sizestr:='dword ';
  148. S_IQ : sizestr:='qword ';
  149. S_FS : sizestr:='dword ';
  150. S_FL : sizestr:='qword ';
  151. S_FX : sizestr:='tword ';
  152. S_BW : if dest then
  153. sizestr:='word '
  154. else
  155. sizestr:='byte ';
  156. S_BL : if dest then
  157. sizestr:='dword '
  158. else
  159. sizestr:='byte ';
  160. S_WL : if dest then
  161. sizestr:='dword '
  162. else
  163. sizestr:='word ';
  164. else { S_NO }
  165. sizestr:='';
  166. end;
  167. end;
  168. Function PadTabs(const p:string;addch:char):string;
  169. var
  170. s : string;
  171. i : longint;
  172. begin
  173. i:=length(p);
  174. if addch<>#0 then
  175. begin
  176. inc(i);
  177. s:=p+addch;
  178. end
  179. else
  180. s:=p;
  181. if i<8 then
  182. PadTabs:=s+#9#9
  183. else
  184. PadTabs:=s+#9;
  185. end;
  186. {****************************************************************************
  187. T386NasmAssembler
  188. ****************************************************************************}
  189. procedure T386NasmAssembler.WriteReference(var ref : treference);
  190. var
  191. first,no_s,no_b,no_i : boolean;
  192. begin
  193. with ref do
  194. begin
  195. no_s:=(segment.enum=R_NO) or ((segment.enum=R_INTREGISTER) and (segment.number=NR_NO));
  196. no_b:=(base.enum=R_NO) or ((base.enum=R_INTREGISTER) and (base.number=NR_NO));
  197. no_i:=(index.enum=R_NO) or ((index.enum=R_INTREGISTER) and (index.number=NR_NO));
  198. AsmWrite('[');
  199. first:=true;
  200. inc(offset,offsetfixup);
  201. offsetfixup:=0;
  202. if not no_s then
  203. if segment.enum=R_INTREGISTER then
  204. asmwrite(intel_regname(segment.number))
  205. else
  206. asmwrite(std_reg2str[segment.enum]+':');
  207. if assigned(symbol) then
  208. begin
  209. AsmWrite(symbol.name);
  210. first:=false;
  211. end;
  212. if not no_b then
  213. begin
  214. if not(first) then
  215. AsmWrite('+')
  216. else
  217. first:=false;
  218. if base.enum=R_INTREGISTER then
  219. asmwrite(intel_regname(base.number))
  220. else
  221. asmwrite(int_nasmreg2str[base.enum]);
  222. end;
  223. if not no_i then
  224. begin
  225. if not(first) then
  226. AsmWrite('+')
  227. else
  228. first:=false;
  229. if index.enum=R_INTREGISTER then
  230. asmwrite(intel_regname(index.number))
  231. else
  232. AsmWrite(int_nasmreg2str[index.enum]);
  233. if scalefactor<>0 then
  234. AsmWrite('*'+tostr(scalefactor));
  235. end;
  236. if offset<0 then
  237. begin
  238. AsmWrite(tostr(offset));
  239. first:=false;
  240. end
  241. else if (offset>0) then
  242. begin
  243. AsmWrite('+'+tostr(offset));
  244. first:=false;
  245. end;
  246. if first then
  247. AsmWrite('0');
  248. AsmWrite(']');
  249. end;
  250. end;
  251. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  252. begin
  253. case o.typ of
  254. top_reg :
  255. begin
  256. if o.reg.enum=R_INTREGISTER then
  257. asmwrite(intel_regname(o.reg.number))
  258. else
  259. asmwrite(int_nasmreg2str[o.reg.enum]);
  260. end;
  261. top_const :
  262. begin
  263. if (ops=1) and (opcode<>A_RET) then
  264. AsmWrite(sizestr(s,dest));
  265. AsmWrite(tostr(longint(o.val)));
  266. end;
  267. top_symbol :
  268. begin
  269. AsmWrite('dword ');
  270. if assigned(o.sym) then
  271. AsmWrite(o.sym.name);
  272. if o.symofs>0 then
  273. AsmWrite('+'+tostr(o.symofs))
  274. else
  275. if o.symofs<0 then
  276. AsmWrite(tostr(o.symofs))
  277. else
  278. if not(assigned(o.sym)) then
  279. AsmWrite('0');
  280. end;
  281. top_ref :
  282. begin
  283. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  284. (opcode = A_LSS) or (opcode = A_LFS) or
  285. (opcode = A_LES) or (opcode = A_LDS) or
  286. (opcode = A_SHR) or (opcode = A_SHL) or
  287. (opcode = A_SAR) or (opcode = A_SAL) or
  288. (opcode = A_OUT) or (opcode = A_IN)) then
  289. AsmWrite(sizestr(s,dest));
  290. WriteReference(o.ref^);
  291. end;
  292. else
  293. internalerror(10001);
  294. end;
  295. end;
  296. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  297. begin
  298. case o.typ of
  299. top_reg :
  300. begin
  301. if o.reg.enum=R_INTREGISTER then
  302. asmwrite(intel_regname(o.reg.number))
  303. else
  304. asmwrite(int_nasmreg2str[o.reg.enum]);
  305. end;
  306. top_ref :
  307. WriteReference(o.ref^);
  308. top_const :
  309. AsmWrite(tostr(longint(o.val)));
  310. top_symbol :
  311. begin
  312. if not(
  313. (op=A_JCXZ) or (op=A_JECXZ) or
  314. (op=A_LOOP) or (op=A_LOOPE) or
  315. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  316. (op=A_LOOPZ)
  317. ) then
  318. AsmWrite('NEAR ');
  319. AsmWrite(o.sym.name);
  320. if o.symofs>0 then
  321. AsmWrite('+'+tostr(o.symofs))
  322. else
  323. if o.symofs<0 then
  324. AsmWrite(tostr(o.symofs));
  325. end;
  326. else
  327. internalerror(10001);
  328. end;
  329. end;
  330. var
  331. LasTSec : TSection;
  332. const
  333. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  334. (#9'DD'#9,#9'DW'#9,#9'DB'#9);
  335. procedure T386NasmAssembler.WriteTree(p:taasmoutput);
  336. const
  337. allocstr : array[boolean] of string[10]=(' released',' allocated');
  338. var
  339. s : string;
  340. hp : tai;
  341. hp1 : tailineinfo;
  342. counter,
  343. lines,
  344. i,j,l : longint;
  345. InlineLevel : longint;
  346. consttyp : taitype;
  347. found,
  348. do_line,
  349. quoted : boolean;
  350. regstr:string[5];
  351. begin
  352. if not assigned(p) then
  353. exit;
  354. InlineLevel:=0;
  355. { lineinfo is only needed for codesegment (PFV) }
  356. do_line:=(cs_asm_source in aktglobalswitches) or
  357. ((cs_lineinfo in aktmoduleswitches)
  358. and (p=codesegment));
  359. hp:=tai(p.first);
  360. while assigned(hp) do
  361. begin
  362. if not(hp.typ in SkipLineInfo) then
  363. begin
  364. hp1:=hp as tailineinfo;
  365. aktfilepos:=hp1.fileinfo;
  366. if do_line then
  367. begin
  368. { load infile }
  369. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  370. begin
  371. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  372. if assigned(infile) then
  373. begin
  374. { open only if needed !! }
  375. if (cs_asm_source in aktglobalswitches) then
  376. infile.open;
  377. end;
  378. { avoid unnecessary reopens of the same file !! }
  379. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  380. { be sure to change line !! }
  381. lastfileinfo.line:=-1;
  382. end;
  383. { write source }
  384. if (cs_asm_source in aktglobalswitches) and
  385. assigned(infile) then
  386. begin
  387. if (infile<>lastinfile) then
  388. begin
  389. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  390. if assigned(lastinfile) then
  391. lastinfile.close;
  392. end;
  393. if (hp1.fileinfo.line<>lastfileinfo.line) and
  394. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  395. begin
  396. if (hp1.fileinfo.line<>0) and
  397. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  398. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  399. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  400. { set it to a negative value !
  401. to make that is has been read already !! PM }
  402. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  403. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  404. end;
  405. end;
  406. lastfileinfo:=hp1.fileinfo;
  407. lastinfile:=infile;
  408. end;
  409. end;
  410. case hp.typ of
  411. ait_comment :
  412. Begin
  413. AsmWrite(target_asm.comment);
  414. AsmWritePChar(tai_comment(hp).str);
  415. AsmLn;
  416. End;
  417. ait_regalloc :
  418. begin
  419. if tai_regalloc(hp).reg.enum=R_INTREGISTER then
  420. regstr:=intel_regname(tai_regalloc(hp).reg.number)
  421. else
  422. regstr:=std_reg2str[tai_regalloc(hp).reg.enum];
  423. if (cs_asm_regalloc in aktglobalswitches) then
  424. AsmWriteLn(#9#9+target_asm.comment+'Register '+regstr+
  425. allocstr[tai_regalloc(hp).allocation]);
  426. end;
  427. ait_tempalloc :
  428. begin
  429. if (cs_asm_tempalloc in aktglobalswitches) then
  430. begin
  431. {$ifdef EXTDEBUG}
  432. if assigned(tai_tempalloc(hp).problem) then
  433. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  434. tostr(tai_tempalloc(hp).tempsize)+')')
  435. else
  436. {$endif EXTDEBUG}
  437. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  438. tostr(tai_tempalloc(hp).tempsize)+allocstr[tai_tempalloc(hp).allocation]);
  439. end;
  440. end;
  441. ait_section :
  442. begin
  443. if tai_section(hp).sec<>sec_none then
  444. begin
  445. AsmLn;
  446. AsmWriteLn('SECTION '+target_asm.secnames[tai_section(hp).sec]);
  447. end;
  448. LasTSec:=tai_section(hp).sec;
  449. end;
  450. ait_align :
  451. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  452. ait_datablock :
  453. begin
  454. if tai_datablock(hp).is_global then
  455. begin
  456. AsmWrite(#9'GLOBAL ');
  457. AsmWriteLn(tai_datablock(hp).sym.name);
  458. end;
  459. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  460. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  461. end;
  462. ait_const_32bit,
  463. ait_const_16bit,
  464. ait_const_8bit :
  465. begin
  466. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  467. consttyp:=hp.typ;
  468. l:=0;
  469. repeat
  470. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  471. if found then
  472. begin
  473. hp:=tai(hp.next);
  474. s:=','+tostr(tai_const(hp).value);
  475. AsmWrite(s);
  476. inc(l,length(s));
  477. end;
  478. until (not found) or (l>line_length);
  479. AsmLn;
  480. end;
  481. ait_const_symbol :
  482. begin
  483. AsmWrite(#9#9'DD'#9);
  484. AsmWrite(tai_const_symbol(hp).sym.name);
  485. if tai_const_symbol(hp).offset>0 then
  486. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  487. else if tai_const_symbol(hp).offset<0 then
  488. AsmWrite(tostr(tai_const_symbol(hp).offset));
  489. AsmLn;
  490. end;
  491. ait_const_rva :
  492. begin
  493. AsmWrite(#9#9'RVA'#9);
  494. AsmWriteLn(tai_const_symbol(hp).sym.name);
  495. end;
  496. ait_real_32bit :
  497. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  498. ait_real_64bit :
  499. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  500. ait_real_80bit :
  501. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  502. ait_comp_64bit :
  503. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  504. ait_string :
  505. begin
  506. counter := 0;
  507. lines := tai_string(hp).len div line_length;
  508. { separate lines in different parts }
  509. if tai_string(hp).len > 0 then
  510. Begin
  511. for j := 0 to lines-1 do
  512. begin
  513. AsmWrite(#9#9'DB'#9);
  514. quoted:=false;
  515. for i:=counter to counter+line_length-1 do
  516. begin
  517. { it is an ascii character. }
  518. if (ord(tai_string(hp).str[i])>31) and
  519. (ord(tai_string(hp).str[i])<128) and
  520. (tai_string(hp).str[i]<>'"') then
  521. begin
  522. if not(quoted) then
  523. begin
  524. if i>counter then
  525. AsmWrite(',');
  526. AsmWrite('"');
  527. end;
  528. AsmWrite(tai_string(hp).str[i]);
  529. quoted:=true;
  530. end { if > 31 and < 128 and ord('"') }
  531. else
  532. begin
  533. if quoted then
  534. AsmWrite('"');
  535. if i>counter then
  536. AsmWrite(',');
  537. quoted:=false;
  538. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  539. end;
  540. end; { end for i:=0 to... }
  541. if quoted then AsmWrite('"');
  542. AsmWrite(target_info.newline);
  543. inc(counter,line_length);
  544. end; { end for j:=0 ... }
  545. { do last line of lines }
  546. if counter<tai_string(hp).len then
  547. AsmWrite(#9#9'DB'#9);
  548. quoted:=false;
  549. for i:=counter to tai_string(hp).len-1 do
  550. begin
  551. { it is an ascii character. }
  552. if (ord(tai_string(hp).str[i])>31) and
  553. (ord(tai_string(hp).str[i])<128) and
  554. (tai_string(hp).str[i]<>'"') then
  555. begin
  556. if not(quoted) then
  557. begin
  558. if i>counter then
  559. AsmWrite(',');
  560. AsmWrite('"');
  561. end;
  562. AsmWrite(tai_string(hp).str[i]);
  563. quoted:=true;
  564. end { if > 31 and < 128 and " }
  565. else
  566. begin
  567. if quoted then
  568. AsmWrite('"');
  569. if i>counter then
  570. AsmWrite(',');
  571. quoted:=false;
  572. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  573. end;
  574. end; { end for i:=0 to... }
  575. if quoted then
  576. AsmWrite('"');
  577. end;
  578. AsmLn;
  579. end;
  580. ait_label :
  581. begin
  582. if tai_label(hp).l.is_used then
  583. AsmWriteLn(tai_label(hp).l.name+':');
  584. end;
  585. ait_direct :
  586. begin
  587. AsmWritePChar(tai_direct(hp).str);
  588. AsmLn;
  589. end;
  590. ait_symbol :
  591. begin
  592. if tai_symbol(hp).is_global then
  593. begin
  594. AsmWrite(#9'GLOBAL ');
  595. AsmWriteLn(tai_symbol(hp).sym.name);
  596. end;
  597. AsmWrite(tai_symbol(hp).sym.name);
  598. if assigned(hp.next) and not(tai(hp.next).typ in
  599. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  600. ait_const_symbol,ait_const_rva,
  601. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  602. AsmWriteLn(':')
  603. end;
  604. ait_symbol_end : ;
  605. ait_instruction :
  606. begin
  607. taicpu(hp).CheckNonCommutativeOpcodes;
  608. { We need intel order, no At&t }
  609. taicpu(hp).SetOperandOrder(op_intel);
  610. s:='';
  611. if ((taicpu(hp).opcode=A_FADDP) or
  612. (taicpu(hp).opcode=A_FMULP))
  613. and (taicpu(hp).ops=0) then
  614. begin
  615. taicpu(hp).ops:=2;
  616. taicpu(hp).oper[0].typ:=top_reg;
  617. taicpu(hp).oper[0].reg.enum:=R_ST1;
  618. taicpu(hp).oper[1].typ:=top_reg;
  619. taicpu(hp).oper[1].reg.enum:=R_ST;
  620. end;
  621. if taicpu(hp).opcode=A_FWAIT then
  622. AsmWriteln(#9#9'DB'#9'09bh')
  623. else
  624. begin
  625. { We need to explicitely set
  626. word prefix to get selectors
  627. to be pushed in 2 bytes PM }
  628. if (taicpu(hp).opsize=S_W) and
  629. ((taicpu(hp).opcode=A_PUSH) or
  630. (taicpu(hp).opcode=A_POP)) and
  631. (taicpu(hp).oper[0].typ=top_reg) and
  632. ((taicpu(hp).oper[0].reg.enum in [firstsreg..lastsreg])) then
  633. AsmWriteln(#9#9'DB'#9'066h');
  634. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  635. if taicpu(hp).ops<>0 then
  636. begin
  637. if is_calljmp(taicpu(hp).opcode) then
  638. begin
  639. AsmWrite(#9);
  640. WriteOper_jmp(taicpu(hp).oper[0],taicpu(hp).opcode);
  641. end
  642. else
  643. begin
  644. for i:=0 to taicpu(hp).ops-1 do
  645. begin
  646. if i=0 then
  647. AsmWrite(#9)
  648. else
  649. AsmWrite(',');
  650. WriteOper(taicpu(hp).oper[i],taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  651. end;
  652. end;
  653. end;
  654. AsmLn;
  655. end;
  656. end;
  657. {$ifdef GDB}
  658. ait_stabn,
  659. ait_stabs,
  660. ait_force_line,
  661. ait_stab_function_name : ;
  662. {$endif GDB}
  663. ait_cut :
  664. begin
  665. { only reset buffer if nothing has changed }
  666. if AsmSize=AsmStartSize then
  667. AsmClear
  668. else
  669. begin
  670. AsmClose;
  671. DoAssemble;
  672. AsmCreate(tai_cut(hp).place);
  673. end;
  674. { avoid empty files }
  675. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  676. begin
  677. if tai(hp.next).typ=ait_section then
  678. lasTSec:=tai_section(hp.next).sec;
  679. hp:=tai(hp.next);
  680. end;
  681. if lasTSec<>sec_none then
  682. AsmWriteLn('SECTION '+target_asm.secnames[lasTSec]);
  683. AsmStartSize:=AsmSize;
  684. end;
  685. ait_marker :
  686. if tai_marker(hp).kind=InlineStart then
  687. inc(InlineLevel)
  688. else if tai_marker(hp).kind=InlineEnd then
  689. dec(InlineLevel);
  690. else
  691. internalerror(10000);
  692. end;
  693. hp:=tai(hp.next);
  694. end;
  695. end;
  696. var
  697. currentasmlist : TExternalAssembler;
  698. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  699. begin
  700. if tasmsymbol(p).defbind=AB_EXTERNAL then
  701. currentasmlist.AsmWriteln('EXTERN'#9+p.name);
  702. end;
  703. procedure T386NasmAssembler.WriteExternals;
  704. begin
  705. currentasmlist:=self;
  706. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  707. end;
  708. procedure T386NasmAssembler.WriteAsmList;
  709. begin
  710. {$ifdef EXTDEBUG}
  711. if assigned(current_module.mainsource) then
  712. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  713. {$endif}
  714. LasTSec:=sec_none;
  715. AsmWriteLn('BITS 32');
  716. AsmLn;
  717. lastfileinfo.line:=-1;
  718. lastfileinfo.fileindex:=0;
  719. lastinfile:=nil;
  720. WriteExternals;
  721. { Nasm doesn't support stabs
  722. WriteTree(debuglist);}
  723. WriteTree(codesegment);
  724. WriteTree(datasegment);
  725. WriteTree(consts);
  726. WriteTree(rttilist);
  727. WriteTree(resourcestringlist);
  728. WriteTree(bsssegment);
  729. Writetree(importssection);
  730. { exports are written by DLLTOOL
  731. if we use it so don't insert it twice (PM) }
  732. if not UseDeffileForExport and assigned(exportssection) then
  733. Writetree(exportssection);
  734. Writetree(resourcesection);
  735. AsmLn;
  736. {$ifdef EXTDEBUG}
  737. if assigned(current_module.mainsource) then
  738. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  739. {$endif EXTDEBUG}
  740. end;
  741. {*****************************************************************************
  742. Initialize
  743. *****************************************************************************}
  744. const
  745. as_i386_nasmcoff_info : tasminfo =
  746. (
  747. id : as_i386_nasmcoff;
  748. idtxt : 'NASMCOFF';
  749. asmbin : 'nasm';
  750. asmcmd : '-f coff -o $OBJ $ASM';
  751. supported_target : system_i386_go32v2;
  752. outputbinary: false;
  753. allowdirect : true;
  754. needar : true;
  755. labelprefix_only_inside_procedure: false;
  756. labelprefix : '..@';
  757. comment : '; ';
  758. secnames : ('',
  759. '.text','.data','.bss',
  760. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  761. '.stab','.stabstr','')
  762. );
  763. as_i386_nasmwin32_info : tasminfo =
  764. (
  765. id : as_i386_nasmwin32;
  766. idtxt : 'NASMWIN32';
  767. asmbin : 'nasm';
  768. asmcmd : '-f win32 -o $OBJ $ASM';
  769. supported_target : system_i386_win32;
  770. outputbinary: false;
  771. allowdirect : true;
  772. needar : true;
  773. labelprefix_only_inside_procedure: false;
  774. labelprefix : '..@';
  775. comment : '; ';
  776. secnames : ('',
  777. '.text','.data','.bss',
  778. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  779. '.stab','.stabstr','')
  780. );
  781. as_i386_nasmobj_info : tasminfo =
  782. (
  783. id : as_i386_nasmobj;
  784. idtxt : 'NASMOBJ';
  785. asmbin : 'nasm';
  786. asmcmd : '-f obj -o $OBJ $ASM';
  787. supported_target : system_any; { what should I write here ?? }
  788. outputbinary: false;
  789. allowdirect : true;
  790. needar : true;
  791. labelprefix_only_inside_procedure: false;
  792. labelprefix : '..@';
  793. comment : '; ';
  794. secnames : ('',
  795. '.text','.data','.bss',
  796. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  797. '.stab','.stabstr','')
  798. );
  799. as_i386_nasmwdosx_info : tasminfo =
  800. (
  801. id : as_i386_nasmwdosx;
  802. idtxt : 'NASMWDOSX';
  803. asmbin : 'nasm';
  804. asmcmd : '-f win32 -o $OBJ $ASM';
  805. supported_target : system_i386_wdosx;
  806. outputbinary: false;
  807. allowdirect : true;
  808. needar : true;
  809. labelprefix_only_inside_procedure: false;
  810. labelprefix : '..@';
  811. comment : '; ';
  812. secnames : ('',
  813. '.text','.data','.bss',
  814. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  815. '.stab','.stabstr','')
  816. );
  817. as_i386_nasmelf_info : tasminfo =
  818. (
  819. id : as_i386_nasmelf;
  820. idtxt : 'NASMELF';
  821. asmbin : 'nasm';
  822. asmcmd : '-f elf -o $OBJ $ASM';
  823. supported_target : system_i386_linux;
  824. outputbinary: false;
  825. allowdirect : true;
  826. needar : true;
  827. labelprefix_only_inside_procedure: false;
  828. labelprefix : '..@';
  829. comment : '; ';
  830. secnames : ('',
  831. '.text','.data','.bss',
  832. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  833. '.stab','.stabstr','')
  834. );
  835. initialization
  836. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  837. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  838. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  839. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  840. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  841. end.
  842. {
  843. $Log$
  844. Revision 1.33 2003-04-22 10:09:35 daniel
  845. + Implemented the actual register allocator
  846. + Scratch registers unavailable when new register allocator used
  847. + maybe_save/maybe_restore unavailable when new register allocator used
  848. Revision 1.32 2003/03/08 13:59:17 daniel
  849. * Work to handle new register notation in ag386nsm
  850. + Added newra version of Ti386moddivnode
  851. Revision 1.31 2003/03/08 08:59:07 daniel
  852. + $define newra will enable new register allocator
  853. + getregisterint will return imaginary registers with $newra
  854. + -sr switch added, will skip register allocation so you can see
  855. the direct output of the code generator before register allocation
  856. Revision 1.30 2003/01/08 18:43:57 daniel
  857. * Tregister changed into a record
  858. Revision 1.29 2002/12/24 18:10:34 peter
  859. * Long symbol names support
  860. Revision 1.28 2002/11/17 16:31:59 carl
  861. * memory optimization (3-4%) : cleanup of tai fields,
  862. cleanup of tdef and tsym fields.
  863. * make it work for m68k
  864. Revision 1.27 2002/11/15 01:58:56 peter
  865. * merged changes from 1.0.7 up to 04-11
  866. - -V option for generating bug report tracing
  867. - more tracing for option parsing
  868. - errors for cdecl and high()
  869. - win32 import stabs
  870. - win32 records<=8 are returned in eax:edx (turned off by default)
  871. - heaptrc update
  872. - more info for temp management in .s file with EXTDEBUG
  873. Revision 1.26 2002/08/18 20:06:28 peter
  874. * inlining is now also allowed in interface
  875. * renamed write/load to ppuwrite/ppuload
  876. * tnode storing in ppu
  877. * nld,ncon,nbas are already updated for storing in ppu
  878. Revision 1.25 2002/08/12 15:08:41 carl
  879. + stab register indexes for powerpc (moved from gdb to cpubase)
  880. + tprocessor enumeration moved to cpuinfo
  881. + linker in target_info is now a class
  882. * many many updates for m68k (will soon start to compile)
  883. - removed some ifdef or correct them for correct cpu
  884. Revision 1.24 2002/08/11 14:32:29 peter
  885. * renamed current_library to objectlibrary
  886. Revision 1.23 2002/08/11 13:24:16 peter
  887. * saving of asmsymbols in ppu supported
  888. * asmsymbollist global is removed and moved into a new class
  889. tasmlibrarydata that will hold the info of a .a file which
  890. corresponds with a single module. Added librarydata to tmodule
  891. to keep the library info stored for the module. In the future the
  892. objectfiles will also be stored to the tasmlibrarydata class
  893. * all getlabel/newasmsymbol and friends are moved to the new class
  894. Revision 1.22 2002/07/26 21:15:43 florian
  895. * rewrote the system handling
  896. Revision 1.21 2002/07/01 18:46:29 peter
  897. * internal linker
  898. * reorganized aasm layer
  899. Revision 1.20 2002/05/18 13:34:21 peter
  900. * readded missing revisions
  901. Revision 1.19 2002/05/16 19:46:50 carl
  902. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  903. + try to fix temp allocation (still in ifdef)
  904. + generic constructor calls
  905. + start of tassembler / tmodulebase class cleanup
  906. Revision 1.17 2002/05/12 16:53:16 peter
  907. * moved entry and exitcode to ncgutil and cgobj
  908. * foreach gets extra argument for passing local data to the
  909. iterator function
  910. * -CR checks also class typecasts at runtime by changing them
  911. into as
  912. * fixed compiler to cycle with the -CR option
  913. * fixed stabs with elf writer, finally the global variables can
  914. be watched
  915. * removed a lot of routines from cga unit and replaced them by
  916. calls to cgobj
  917. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  918. u32bit then the other is typecasted also to u32bit without giving
  919. a rangecheck warning/error.
  920. * fixed pascal calling method with reversing also the high tree in
  921. the parast, detected by tcalcst3 test
  922. Revision 1.16 2002/04/15 19:12:09 carl
  923. + target_info.size_of_pointer -> pointer_size
  924. + some cleanup of unused types/variables
  925. * move several constants from cpubase to their specific units
  926. (where they are used)
  927. + att_Reg2str -> gas_reg2str
  928. + int_reg2str -> std_reg2str
  929. Revision 1.15 2002/04/14 16:58:41 carl
  930. + att_reg2str -> gas_reg2str
  931. Revision 1.14 2002/04/04 18:27:37 carl
  932. + added wdosx support (patch from Pavel)
  933. Revision 1.13 2002/04/02 17:11:33 peter
  934. * tlocation,treference update
  935. * LOC_CONSTANT added for better constant handling
  936. * secondadd splitted in multiple routines
  937. * location_force_reg added for loading a location to a register
  938. of a specified size
  939. * secondassignment parses now first the right and then the left node
  940. (this is compatible with Kylix). This saves a lot of push/pop especially
  941. with string operations
  942. * adapted some routines to use the new cg methods
  943. }