ag386int.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asmoutput class for Intel syntax with Intel 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. {
  19. This unit implements an asmoutput class for Intel syntax with Intel i386+
  20. }
  21. unit ag386int;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cpubase,
  26. aasmbase,aasmtai,aasmcpu,assemble;
  27. type
  28. T386IntelAssembler = class(TExternalAssembler)
  29. private
  30. procedure WriteReference(var ref : treference);
  31. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  32. procedure WriteOper_jmp(const o:toper;s : topsize);
  33. public
  34. procedure WriteTree(p:TAAsmoutput);override;
  35. procedure WriteAsmList;override;
  36. Function DoAssemble:boolean;override;
  37. procedure WriteExternals;
  38. end;
  39. const
  40. regname_count=45;
  41. regname_count_bsstart=32;
  42. intel_regname2regnum:array[0..regname_count-1] of regname2regnumrec=(
  43. (name:'ah'; number:NR_AH),
  44. (name:'al'; number:NR_AL),
  45. (name:'ax'; number:NR_AX),
  46. (name:'bh'; number:NR_BH),
  47. (name:'bl'; number:NR_BL),
  48. (name:'bp'; number:NR_BP),
  49. (name:'bx'; number:NR_BX),
  50. (name:'ch'; number:NR_CH),
  51. (name:'cl'; number:NR_CL),
  52. (name:'cs'; number:NR_CS),
  53. (name:'cr0'; number:NR_CR0),
  54. (name:'cr2'; number:NR_CR2),
  55. (name:'cr3'; number:NR_CR3),
  56. (name:'cr4'; number:NR_CR4),
  57. (name:'cx'; number:NR_CX),
  58. (name:'dh'; number:NR_DH),
  59. (name:'dl'; number:NR_DL),
  60. (name:'di'; number:NR_DI),
  61. (name:'dr0'; number:NR_DR0),
  62. (name:'dr1'; number:NR_DR1),
  63. (name:'dr2'; number:NR_DR2),
  64. (name:'dr3'; number:NR_DR3),
  65. (name:'dr6'; number:NR_DR6),
  66. (name:'dr7'; number:NR_DR7),
  67. (name:'ds'; number:NR_DS),
  68. (name:'dx'; number:NR_DX),
  69. (name:'eax'; number:NR_EAX),
  70. (name:'ebp'; number:NR_EBP),
  71. (name:'ebx'; number:NR_EBX),
  72. (name:'ecx'; number:NR_ECX),
  73. (name:'edi'; number:NR_EDI),
  74. (name:'edx'; number:NR_EDX),
  75. (name:'es'; number:NR_ES),
  76. (name:'esi'; number:NR_ESI),
  77. (name:'esp'; number:NR_ESP),
  78. (name:'fs'; number:NR_FS),
  79. (name:'gs'; number:NR_GS),
  80. (name:'si'; number:NR_SI),
  81. (name:'sp'; number:NR_SP),
  82. (name:'ss'; number:NR_SS),
  83. (name:'tr3'; number:NR_DR0),
  84. (name:'tr4'; number:NR_DR1),
  85. (name:'tr5'; number:NR_DR2),
  86. (name:'tr6'; number:NR_DR6),
  87. (name:'tr7'; number:NR_DR7)
  88. );
  89. function intel_regnum_search(const s:string):Tnewregister;
  90. function intel_regname(const r:Tnewregister):string;
  91. implementation
  92. uses
  93. {$ifdef delphi}
  94. sysutils,
  95. {$endif}
  96. cutils,globtype,globals,systems,cclasses,
  97. verbose,finput,fmodule,script,cpuinfo
  98. ;
  99. const
  100. line_length = 70;
  101. function single2str(d : single) : string;
  102. var
  103. hs : string;
  104. p : byte;
  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. single2str:=lower(hs);
  115. end;
  116. function double2str(d : double) : string;
  117. var
  118. hs : string;
  119. p : byte;
  120. begin
  121. str(d,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. double2str:=lower(hs);
  130. end;
  131. function extended2str(e : extended) : string;
  132. var
  133. hs : string;
  134. p : byte;
  135. begin
  136. str(e,hs);
  137. { nasm expects a lowercase e }
  138. p:=pos('E',hs);
  139. if p>0 then
  140. hs[p]:='e';
  141. p:=pos('+',hs);
  142. if p>0 then
  143. delete(hs,p,1);
  144. extended2str:=lower(hs);
  145. end;
  146. function comp2str(d : bestreal) : string;
  147. type
  148. pdouble = ^double;
  149. var
  150. c : comp;
  151. dd : pdouble;
  152. begin
  153. {$ifdef FPC}
  154. c:=comp(d);
  155. {$else}
  156. c:=d;
  157. {$endif}
  158. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  159. comp2str:=double2str(dd^);
  160. end;
  161. function fixline(s:string):string;
  162. {
  163. return s with all leading and ending spaces and tabs removed
  164. }
  165. var
  166. i,j,k : longint;
  167. begin
  168. i:=length(s);
  169. while (i>0) and (s[i] in [#9,' ']) do
  170. dec(i);
  171. j:=1;
  172. while (j<i) and (s[j] in [#9,' ']) do
  173. inc(j);
  174. for k:=j to i do
  175. if s[k] in [#0..#31,#127..#255] then
  176. s[k]:='.';
  177. fixline:=Copy(s,j,i-j+1);
  178. end;
  179. {****************************************************************************
  180. T386IntelAssembler
  181. ****************************************************************************}
  182. procedure T386IntelAssembler.WriteReference(var ref : treference);
  183. var
  184. first : boolean;
  185. begin
  186. with ref do
  187. begin
  188. if segment.enum>lastreg then
  189. internalerror(200301081);
  190. if base.enum>lastreg then
  191. internalerror(200301081);
  192. if index.enum>lastreg then
  193. internalerror(200301081);
  194. first:=true;
  195. inc(offset,offsetfixup);
  196. offsetfixup:=0;
  197. if segment.enum<>R_NO then
  198. AsmWrite(std_reg2str[segment.enum]+':[')
  199. else
  200. AsmWrite('[');
  201. if assigned(symbol) then
  202. begin
  203. if (aktoutputformat = as_i386_tasm) then
  204. AsmWrite('dword ptr ');
  205. AsmWrite(symbol.name);
  206. first:=false;
  207. end;
  208. if (base.enum<>R_NO) then
  209. begin
  210. if not(first) then
  211. AsmWrite('+')
  212. else
  213. first:=false;
  214. AsmWrite(std_reg2str[base.enum]);
  215. end;
  216. if (index.enum<>R_NO) then
  217. begin
  218. if not(first) then
  219. AsmWrite('+')
  220. else
  221. first:=false;
  222. AsmWrite(std_reg2str[index.enum]);
  223. if scalefactor<>0 then
  224. AsmWrite('*'+tostr(scalefactor));
  225. end;
  226. if offset<0 then
  227. begin
  228. AsmWrite(tostr(offset));
  229. first:=false;
  230. end
  231. else if (offset>0) then
  232. begin
  233. AsmWrite('+'+tostr(offset));
  234. first:=false;
  235. end;
  236. if first then
  237. AsmWrite('0');
  238. AsmWrite(']');
  239. end;
  240. end;
  241. procedure T386IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  242. begin
  243. case o.typ of
  244. top_reg :
  245. begin
  246. if o.reg.enum>lastreg then
  247. internalerror(200301081);
  248. AsmWrite(std_reg2str[o.reg.enum]);
  249. end;
  250. top_const :
  251. AsmWrite(tostr(longint(o.val)));
  252. top_symbol :
  253. begin
  254. AsmWrite('offset ');
  255. if assigned(o.sym) then
  256. AsmWrite(o.sym.name);
  257. if o.symofs>0 then
  258. AsmWrite('+'+tostr(o.symofs))
  259. else
  260. if o.symofs<0 then
  261. AsmWrite(tostr(o.symofs))
  262. else
  263. if not(assigned(o.sym)) then
  264. AsmWrite('0');
  265. end;
  266. top_ref :
  267. begin
  268. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  269. (opcode <> A_LFS) and (opcode <> A_LDS) and
  270. (opcode <> A_LES)) then
  271. Begin
  272. case s of
  273. S_B : AsmWrite('byte ptr ');
  274. S_W : AsmWrite('word ptr ');
  275. S_L : AsmWrite('dword ptr ');
  276. S_IS : AsmWrite('word ptr ');
  277. S_IL : AsmWrite('dword ptr ');
  278. S_IQ : AsmWrite('qword ptr ');
  279. S_FS : AsmWrite('dword ptr ');
  280. S_FL : AsmWrite('qword ptr ');
  281. S_FX : AsmWrite('tbyte ptr ');
  282. S_BW : if dest then
  283. AsmWrite('word ptr ')
  284. else
  285. AsmWrite('byte ptr ');
  286. S_BL : if dest then
  287. AsmWrite('dword ptr ')
  288. else
  289. AsmWrite('byte ptr ');
  290. S_WL : if dest then
  291. AsmWrite('dword ptr ')
  292. else
  293. AsmWrite('word ptr ');
  294. end;
  295. end;
  296. WriteReference(o.ref^);
  297. end;
  298. else
  299. internalerror(10001);
  300. end;
  301. end;
  302. procedure T386IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  303. begin
  304. case o.typ of
  305. top_reg :
  306. begin
  307. if o.reg.enum>lastreg then
  308. internalerror(200301081);
  309. AsmWrite(std_reg2str[o.reg.enum]);
  310. end;
  311. top_const :
  312. AsmWrite(tostr(longint(o.val)));
  313. top_symbol :
  314. begin
  315. AsmWrite(o.sym.name);
  316. if o.symofs>0 then
  317. AsmWrite('+'+tostr(o.symofs))
  318. else
  319. if o.symofs<0 then
  320. AsmWrite(tostr(o.symofs));
  321. end;
  322. top_ref :
  323. { what about lcall or ljmp ??? }
  324. begin
  325. if (aktoutputformat <> as_i386_tasm) then
  326. begin
  327. if s=S_FAR then
  328. AsmWrite('far ptr ')
  329. else
  330. AsmWrite('dword ptr ');
  331. end;
  332. WriteReference(o.ref^);
  333. end;
  334. else
  335. internalerror(10001);
  336. end;
  337. end;
  338. var
  339. LasTSec : TSection;
  340. lastfileinfo : tfileposinfo;
  341. infile,
  342. lastinfile : tinputfile;
  343. const
  344. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  345. (#9'DD'#9,#9'DW'#9,#9'DB'#9);
  346. Function PadTabs(const p:string;addch:char):string;
  347. var
  348. s : string;
  349. i : longint;
  350. begin
  351. i:=length(p);
  352. if addch<>#0 then
  353. begin
  354. inc(i);
  355. s:=p+addch;
  356. end
  357. else
  358. s:=p;
  359. if i<8 then
  360. PadTabs:=s+#9#9
  361. else
  362. PadTabs:=s+#9;
  363. end;
  364. procedure T386IntelAssembler.WriteTree(p:TAAsmoutput);
  365. const
  366. allocstr : array[boolean] of string[10]=(' released',' allocated');
  367. var
  368. s,
  369. prefix,
  370. suffix : string;
  371. hp : tai;
  372. hp1 : tailineinfo;
  373. counter,
  374. lines,
  375. InlineLevel : longint;
  376. i,j,l : longint;
  377. consttyp : taitype;
  378. found,
  379. do_line,DoNotSplitLine,
  380. quoted : boolean;
  381. begin
  382. if not assigned(p) then
  383. exit;
  384. { lineinfo is only needed for codesegment (PFV) }
  385. do_line:=((cs_asm_source in aktglobalswitches) or
  386. (cs_lineinfo in aktmoduleswitches))
  387. and (p=codesegment);
  388. InlineLevel:=0;
  389. DoNotSplitLine:=false;
  390. hp:=tai(p.first);
  391. while assigned(hp) do
  392. begin
  393. if do_line and not(hp.typ in SkipLineInfo) and
  394. not DoNotSplitLine then
  395. begin
  396. hp1:=hp as tailineinfo;
  397. { load infile }
  398. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  399. begin
  400. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  401. if assigned(infile) then
  402. begin
  403. { open only if needed !! }
  404. if (cs_asm_source in aktglobalswitches) then
  405. infile.open;
  406. end;
  407. { avoid unnecessary reopens of the same file !! }
  408. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  409. { be sure to change line !! }
  410. lastfileinfo.line:=-1;
  411. end;
  412. { write source }
  413. if (cs_asm_source in aktglobalswitches) and
  414. assigned(infile) then
  415. begin
  416. if (infile<>lastinfile) then
  417. begin
  418. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  419. if assigned(lastinfile) then
  420. lastinfile.close;
  421. end;
  422. if (hp1.fileinfo.line<>lastfileinfo.line) and
  423. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  424. begin
  425. if (hp1.fileinfo.line<>0) and
  426. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  427. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  428. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  429. { set it to a negative value !
  430. to make that is has been read already !! PM }
  431. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  432. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  433. end;
  434. end;
  435. lastfileinfo:=hp1.fileinfo;
  436. lastinfile:=infile;
  437. end;
  438. DoNotSplitLine:=false;
  439. case hp.typ of
  440. ait_comment : Begin
  441. AsmWrite(target_asm.comment);
  442. AsmWritePChar(tai_comment(hp).str);
  443. AsmLn;
  444. End;
  445. ait_regalloc :
  446. begin
  447. if (cs_asm_regalloc in aktglobalswitches) then
  448. AsmWriteLn(target_asm.comment+'Register '+std_reg2str[tai_regalloc(hp).reg.enum]+
  449. allocstr[tai_regalloc(hp).allocation]);
  450. end;
  451. ait_tempalloc :
  452. begin
  453. if (cs_asm_tempalloc in aktglobalswitches) then
  454. begin
  455. {$ifdef EXTDEBUG}
  456. if assigned(tai_tempalloc(hp).problem) then
  457. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  458. tostr(tai_tempalloc(hp).tempsize)+')')
  459. else
  460. {$endif EXTDEBUG}
  461. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  462. tostr(tai_tempalloc(hp).tempsize)+allocstr[tai_tempalloc(hp).allocation]);
  463. end;
  464. end;
  465. ait_section : begin
  466. if LasTSec<>sec_none then
  467. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');
  468. if tai_section(hp).sec<>sec_none then
  469. begin
  470. AsmLn;
  471. AsmWriteLn('_'+target_asm.secnames[tai_section(hp).sec]+#9#9+
  472. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  473. target_asm.secnames[tai_section(hp).sec]+'''');
  474. end;
  475. LasTSec:=tai_section(hp).sec;
  476. end;
  477. ait_align : begin
  478. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  479. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  480. { HERE UNDER TASM! }
  481. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  482. end;
  483. ait_datablock : begin
  484. if tai_datablock(hp).is_global then
  485. AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  486. AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  487. end;
  488. ait_const_32bit,
  489. ait_const_8bit,
  490. ait_const_16bit : begin
  491. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  492. consttyp:=hp.typ;
  493. l:=0;
  494. repeat
  495. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  496. if found then
  497. begin
  498. hp:=tai(hp.next);
  499. s:=','+tostr(tai_const(hp).value);
  500. AsmWrite(s);
  501. inc(l,length(s));
  502. end;
  503. until (not found) or (l>line_length);
  504. AsmLn;
  505. end;
  506. ait_const_symbol : begin
  507. AsmWriteLn(#9#9'DD'#9'offset '+tai_const_symbol(hp).sym.name);
  508. if tai_const_symbol(hp).offset>0 then
  509. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  510. else if tai_const_symbol(hp).offset<0 then
  511. AsmWrite(tostr(tai_const_symbol(hp).offset));
  512. AsmLn;
  513. end;
  514. ait_const_rva : begin
  515. AsmWriteLn(#9#9'RVA'#9+tai_const_symbol(hp).sym.name);
  516. end;
  517. ait_real_32bit : AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  518. ait_real_64bit : AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  519. ait_real_80bit : AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  520. ait_comp_64bit : AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  521. ait_string : begin
  522. counter := 0;
  523. lines := tai_string(hp).len div line_length;
  524. { separate lines in different parts }
  525. if tai_string(hp).len > 0 then
  526. Begin
  527. for j := 0 to lines-1 do
  528. begin
  529. AsmWrite(#9#9'DB'#9);
  530. quoted:=false;
  531. for i:=counter to counter+line_length do
  532. begin
  533. { it is an ascii character. }
  534. if (ord(tai_string(hp).str[i])>31) and
  535. (ord(tai_string(hp).str[i])<128) and
  536. (tai_string(hp).str[i]<>'"') then
  537. begin
  538. if not(quoted) then
  539. begin
  540. if i>counter then
  541. AsmWrite(',');
  542. AsmWrite('"');
  543. end;
  544. AsmWrite(tai_string(hp).str[i]);
  545. quoted:=true;
  546. end { if > 31 and < 128 and ord('"') }
  547. else
  548. begin
  549. if quoted then
  550. AsmWrite('"');
  551. if i>counter then
  552. AsmWrite(',');
  553. quoted:=false;
  554. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  555. end;
  556. end; { end for i:=0 to... }
  557. if quoted then AsmWrite('"');
  558. AsmWrite(target_info.newline);
  559. counter := counter+line_length;
  560. end; { end for j:=0 ... }
  561. { do last line of lines }
  562. AsmWrite(#9#9'DB'#9);
  563. quoted:=false;
  564. for i:=counter to tai_string(hp).len-1 do
  565. begin
  566. { it is an ascii character. }
  567. if (ord(tai_string(hp).str[i])>31) and
  568. (ord(tai_string(hp).str[i])<128) and
  569. (tai_string(hp).str[i]<>'"') then
  570. begin
  571. if not(quoted) then
  572. begin
  573. if i>counter then
  574. AsmWrite(',');
  575. AsmWrite('"');
  576. end;
  577. AsmWrite(tai_string(hp).str[i]);
  578. quoted:=true;
  579. end { if > 31 and < 128 and " }
  580. else
  581. begin
  582. if quoted then
  583. AsmWrite('"');
  584. if i>counter then
  585. AsmWrite(',');
  586. quoted:=false;
  587. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  588. end;
  589. end; { end for i:=0 to... }
  590. if quoted then
  591. AsmWrite('"');
  592. end;
  593. AsmLn;
  594. end;
  595. ait_label : begin
  596. if tai_label(hp).l.is_used then
  597. begin
  598. AsmWrite(tai_label(hp).l.name);
  599. if assigned(hp.next) and not(tai(hp.next).typ in
  600. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  601. ait_const_symbol,ait_const_rva,
  602. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  603. AsmWriteLn(':')
  604. else
  605. DoNotSplitLine:=true;
  606. end;
  607. end;
  608. ait_direct : begin
  609. AsmWritePChar(tai_direct(hp).str);
  610. AsmLn;
  611. end;
  612. ait_symbol : begin
  613. if tai_symbol(hp).is_global then
  614. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  615. AsmWrite(tai_symbol(hp).sym.name);
  616. if assigned(hp.next) and not(tai(hp.next).typ in
  617. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  618. ait_const_symbol,ait_const_rva,
  619. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  620. AsmWriteLn(':')
  621. end;
  622. ait_symbol_end : begin
  623. end;
  624. ait_instruction : begin
  625. taicpu(hp).CheckNonCommutativeOpcodes;
  626. taicpu(hp).SetOperandOrder(op_intel);
  627. { Reset }
  628. suffix:='';
  629. prefix:= '';
  630. { We need to explicitely set
  631. word prefix to get selectors
  632. to be pushed in 2 bytes PM }
  633. if (taicpu(hp).opsize=S_W) and
  634. ((taicpu(hp).opcode=A_PUSH) or
  635. (taicpu(hp).opcode=A_POP)) and
  636. (taicpu(hp).oper[0].typ=top_reg) and
  637. ((taicpu(hp).oper[0].reg.enum in [firstsreg..lastsreg])) then
  638. AsmWriteln(#9#9'DB'#9'066h');
  639. { added prefix instructions, must be on same line as opcode }
  640. if (taicpu(hp).ops = 0) and
  641. ((taicpu(hp).opcode = A_REP) or
  642. (taicpu(hp).opcode = A_LOCK) or
  643. (taicpu(hp).opcode = A_REPE) or
  644. (taicpu(hp).opcode = A_REPNZ) or
  645. (taicpu(hp).opcode = A_REPZ) or
  646. (taicpu(hp).opcode = A_REPNE)) then
  647. Begin
  648. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  649. hp:=tai(hp.next);
  650. { this is theorically impossible... }
  651. if hp=nil then
  652. begin
  653. AsmWriteLn(#9#9+prefix);
  654. break;
  655. end;
  656. { nasm prefers prefix on a line alone
  657. AsmWriteln(#9#9+prefix); but not masm PM
  658. prefix:=''; }
  659. if (aktoutputformat = as_i386_masm) then
  660. begin
  661. AsmWriteln(s);
  662. prefix:='';
  663. end;
  664. end
  665. else
  666. prefix:= '';
  667. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  668. if taicpu(hp).ops<>0 then
  669. begin
  670. if is_calljmp(taicpu(hp).opcode) then
  671. begin
  672. AsmWrite(#9);
  673. WriteOper_jmp(taicpu(hp).oper[0],taicpu(hp).opsize);
  674. end
  675. else
  676. begin
  677. for i:=0to taicpu(hp).ops-1 do
  678. begin
  679. if i=0 then
  680. AsmWrite(#9)
  681. else
  682. AsmWrite(',');
  683. WriteOper(taicpu(hp).oper[i],taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  684. end;
  685. end;
  686. end;
  687. AsmLn;
  688. end;
  689. {$ifdef GDB}
  690. ait_stabn,
  691. ait_stabs,
  692. ait_force_line,
  693. ait_stab_function_name : ;
  694. {$endif GDB}
  695. ait_cut : begin
  696. { only reset buffer if nothing has changed }
  697. if AsmSize=AsmStartSize then
  698. AsmClear
  699. else
  700. begin
  701. if LasTSec<>sec_none then
  702. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');
  703. AsmLn;
  704. AsmWriteLn(#9'END');
  705. AsmClose;
  706. DoAssemble;
  707. AsmCreate(tai_cut(hp).place);
  708. end;
  709. { avoid empty files }
  710. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  711. begin
  712. if tai(hp.next).typ=ait_section then
  713. begin
  714. lasTSec:=tai_section(hp.next).sec;
  715. end;
  716. hp:=tai(hp.next);
  717. end;
  718. AsmWriteLn(#9'.386p');
  719. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  720. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  721. { I was told that this isn't necesarry because }
  722. { the labels generated by FPC are unique (FK) }
  723. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  724. if lasTSec<>sec_none then
  725. AsmWriteLn('_'+target_asm.secnames[lasTSec]+#9#9+
  726. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  727. target_asm.secnames[lasTSec]+'''');
  728. AsmStartSize:=AsmSize;
  729. end;
  730. ait_marker :
  731. begin
  732. if tai_marker(hp).kind=InlineStart then
  733. inc(InlineLevel)
  734. else if tai_marker(hp).kind=InlineEnd then
  735. dec(InlineLevel);
  736. end;
  737. else
  738. internalerror(10000);
  739. end;
  740. hp:=tai(hp.next);
  741. end;
  742. end;
  743. var
  744. currentasmlist : TExternalAssembler;
  745. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  746. begin
  747. if tasmsymbol(p).defbind=AB_EXTERNAL then
  748. begin
  749. if (aktoutputformat = as_i386_masm) then
  750. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  751. +': NEAR')
  752. else
  753. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name);
  754. end;
  755. end;
  756. procedure T386IntelAssembler.WriteExternals;
  757. begin
  758. currentasmlist:=self;
  759. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  760. end;
  761. function t386intelassembler.DoAssemble : boolean;
  762. var f : file;
  763. begin
  764. DoAssemble:=Inherited DoAssemble;
  765. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  766. if (aktoutputformat = as_i386_masm) then
  767. begin
  768. if not(cs_asm_extern in aktglobalswitches) then
  769. begin
  770. if Not FileExists(objfile) and
  771. FileExists(ForceExtension(objfile,'.obj')) then
  772. begin
  773. Assign(F,ForceExtension(objfile,'.obj'));
  774. Rename(F,objfile);
  775. end;
  776. end
  777. else
  778. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  779. end;
  780. end;
  781. procedure T386IntelAssembler.WriteAsmList;
  782. begin
  783. {$ifdef EXTDEBUG}
  784. if assigned(current_module.mainsource) then
  785. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  786. {$endif}
  787. LasTSec:=sec_none;
  788. AsmWriteLn(#9'.386p');
  789. { masm 6.11 does not seem to like LOCALS PM }
  790. if (aktoutputformat = as_i386_tasm) then
  791. begin
  792. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  793. end;
  794. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  795. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  796. AsmLn;
  797. WriteExternals;
  798. { INTEL ASM doesn't support stabs
  799. WriteTree(debuglist);}
  800. WriteTree(codesegment);
  801. WriteTree(datasegment);
  802. WriteTree(consts);
  803. WriteTree(rttilist);
  804. WriteTree(resourcestringlist);
  805. WriteTree(bsssegment);
  806. AsmWriteLn(#9'END');
  807. AsmLn;
  808. {$ifdef EXTDEBUG}
  809. if assigned(current_module.mainsource) then
  810. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  811. {$endif EXTDEBUG}
  812. end;
  813. function intel_regnum_search(const s:string):Tnewregister;
  814. {Searches the register number that belongs to the register in s.
  815. s must be in uppercase!.}
  816. var i,p:byte;
  817. begin
  818. {Binary search.}
  819. p:=0;
  820. i:=regname_count_bsstart;
  821. while i<>0 do
  822. begin
  823. if (p+i<regname_count) and (upper(intel_regname2regnum[p+i].name)<=s) then
  824. p:=p+i;
  825. i:=i shr 1;
  826. end;
  827. if upper(intel_regname2regnum[p].name)=s then
  828. intel_regnum_search:=intel_regname2regnum[p].number
  829. else
  830. intel_regnum_search:=NR_NO;
  831. end;
  832. function intel_regname(const r:Tnewregister):string;
  833. var i:byte;
  834. nr:string[3];
  835. sub:char;
  836. begin
  837. intel_regname:='';
  838. for i:=0 to regname_count-1 do
  839. if r=intel_regname2regnum[i].number then
  840. intel_regname:=intel_regname2regnum[i].name;
  841. {If not found, generate a systematic name.}
  842. if intel_regname='' then
  843. begin
  844. str(r shr 8,nr);
  845. case r and $ff of
  846. R_SUBL:
  847. sub:='l';
  848. R_SUBH:
  849. sub:='h';
  850. R_SUBW:
  851. sub:='w';
  852. R_SUBD:
  853. sub:='d';
  854. R_SUBQ:
  855. sub:='q';
  856. else
  857. internalerror(200303081);
  858. end;
  859. intel_regname:='reg'+nr+sub;
  860. end;
  861. end;
  862. {*****************************************************************************
  863. Initialize
  864. *****************************************************************************}
  865. const
  866. as_i386_tasm_info : tasminfo =
  867. (
  868. id : as_i386_tasm;
  869. idtxt : 'TASM';
  870. asmbin : 'tasm';
  871. asmcmd : '/m2 /ml $ASM $OBJ';
  872. supported_target : system_any; { what should I write here ?? }
  873. outputbinary: false;
  874. allowdirect : true;
  875. needar : true;
  876. labelprefix_only_inside_procedure : true;
  877. labelprefix : '@@';
  878. comment : '; ';
  879. secnames : ('',
  880. 'CODE','DATA','BSS',
  881. '','','','','','',
  882. '','','')
  883. );
  884. as_i386_masm_info : tasminfo =
  885. (
  886. id : as_i386_masm;
  887. idtxt : 'MASM';
  888. asmbin : 'masm';
  889. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  890. supported_target : system_any; { what should I write here ?? }
  891. outputbinary: false;
  892. allowdirect : true;
  893. needar : true;
  894. labelprefix_only_inside_procedure : false;
  895. labelprefix : '@@';
  896. comment : '; ';
  897. secnames : ('',
  898. 'CODE','DATA','BSS',
  899. '','','','','','',
  900. '','','')
  901. );
  902. initialization
  903. RegisterAssembler(as_i386_tasm_info,T386IntelAssembler);
  904. RegisterAssembler(as_i386_masm_info,T386IntelAssembler);
  905. end.
  906. {
  907. $Log$
  908. Revision 1.34 2003-03-08 08:59:07 daniel
  909. + $define newra will enable new register allocator
  910. + getregisterint will return imaginary registers with $newra
  911. + -sr switch added, will skip register allocation so you can see
  912. the direct output of the code generator before register allocation
  913. Revision 1.33 2003/02/19 22:00:15 daniel
  914. * Code generator converted to new register notation
  915. - Horribily outdated todo.txt removed
  916. Revision 1.32 2003/01/08 18:43:57 daniel
  917. * Tregister changed into a record
  918. Revision 1.31 2002/12/24 18:10:34 peter
  919. * Long symbol names support
  920. Revision 1.30 2002/11/17 16:31:58 carl
  921. * memory optimization (3-4%) : cleanup of tai fields,
  922. cleanup of tdef and tsym fields.
  923. * make it work for m68k
  924. Revision 1.29 2002/11/15 01:58:56 peter
  925. * merged changes from 1.0.7 up to 04-11
  926. - -V option for generating bug report tracing
  927. - more tracing for option parsing
  928. - errors for cdecl and high()
  929. - win32 import stabs
  930. - win32 records<=8 are returned in eax:edx (turned off by default)
  931. - heaptrc update
  932. - more info for temp management in .s file with EXTDEBUG
  933. Revision 1.28 2002/08/20 21:40:44 florian
  934. + target macos for ppc added
  935. + frame work for mpw assembler output
  936. Revision 1.27 2002/08/18 20:06:28 peter
  937. * inlining is now also allowed in interface
  938. * renamed write/load to ppuwrite/ppuload
  939. * tnode storing in ppu
  940. * nld,ncon,nbas are already updated for storing in ppu
  941. Revision 1.26 2002/08/12 15:08:41 carl
  942. + stab register indexes for powerpc (moved from gdb to cpubase)
  943. + tprocessor enumeration moved to cpuinfo
  944. + linker in target_info is now a class
  945. * many many updates for m68k (will soon start to compile)
  946. - removed some ifdef or correct them for correct cpu
  947. Revision 1.25 2002/08/11 14:32:29 peter
  948. * renamed current_library to objectlibrary
  949. Revision 1.24 2002/08/11 13:24:16 peter
  950. * saving of asmsymbols in ppu supported
  951. * asmsymbollist global is removed and moved into a new class
  952. tasmlibrarydata that will hold the info of a .a file which
  953. corresponds with a single module. Added librarydata to tmodule
  954. to keep the library info stored for the module. In the future the
  955. objectfiles will also be stored to the tasmlibrarydata class
  956. * all getlabel/newasmsymbol and friends are moved to the new class
  957. Revision 1.23 2002/07/26 21:15:43 florian
  958. * rewrote the system handling
  959. Revision 1.22 2002/07/01 18:46:29 peter
  960. * internal linker
  961. * reorganized aasm layer
  962. Revision 1.21 2002/05/18 13:34:21 peter
  963. * readded missing revisions
  964. Revision 1.20 2002/05/16 19:46:50 carl
  965. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  966. + try to fix temp allocation (still in ifdef)
  967. + generic constructor calls
  968. + start of tassembler / tmodulebase class cleanup
  969. Revision 1.18 2002/05/12 16:53:16 peter
  970. * moved entry and exitcode to ncgutil and cgobj
  971. * foreach gets extra argument for passing local data to the
  972. iterator function
  973. * -CR checks also class typecasts at runtime by changing them
  974. into as
  975. * fixed compiler to cycle with the -CR option
  976. * fixed stabs with elf writer, finally the global variables can
  977. be watched
  978. * removed a lot of routines from cga unit and replaced them by
  979. calls to cgobj
  980. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  981. u32bit then the other is typecasted also to u32bit without giving
  982. a rangecheck warning/error.
  983. * fixed pascal calling method with reversing also the high tree in
  984. the parast, detected by tcalcst3 test
  985. Revision 1.17 2002/04/15 19:12:09 carl
  986. + target_info.size_of_pointer -> pointer_size
  987. + some cleanup of unused types/variables
  988. * move several constants from cpubase to their specific units
  989. (where they are used)
  990. + att_Reg2str -> gas_reg2str
  991. + int_reg2str -> std_reg2str
  992. Revision 1.16 2002/04/04 19:06:07 peter
  993. * removed unused units
  994. * use tlocation.size in cg.a_*loc*() routines
  995. Revision 1.15 2002/04/02 17:11:33 peter
  996. * tlocation,treference update
  997. * LOC_CONSTANT added for better constant handling
  998. * secondadd splitted in multiple routines
  999. * location_force_reg added for loading a location to a register
  1000. of a specified size
  1001. * secondassignment parses now first the right and then the left node
  1002. (this is compatible with Kylix). This saves a lot of push/pop especially
  1003. with string operations
  1004. * adapted some routines to use the new cg methods
  1005. }