ag386bin.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by the FPC development team
  4. This unit implements an binary assembler output class
  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. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif}
  21. unit ag386bin;
  22. {$define MULTIPASS}
  23. {$define EXTERNALBSS}
  24. interface
  25. uses
  26. cpubase,cobjects,aasm,files,assemble;
  27. type
  28. togtype=(og_none,og_dbg,og_coff,og_pecoff);
  29. pi386binasmlist=^ti386binasmlist;
  30. ti386binasmlist=object
  31. SmartAsm : boolean;
  32. constructor init(t:togtype;smart:boolean);
  33. destructor done;
  34. procedure WriteBin;
  35. private
  36. { the aasmoutput lists that need to be processed }
  37. lists : byte;
  38. list : array[1..maxoutputlists] of paasmoutput;
  39. { current processing }
  40. currlistidx : byte;
  41. currlist : paasmoutput;
  42. currpass : byte;
  43. {$ifdef GDB}
  44. n_line : byte; { different types of source lines }
  45. linecount,
  46. includecount : longint;
  47. funcname : pasmsymbol;
  48. stabslastfileinfo : tfileposinfo;
  49. procedure convertstabs(p:pchar);
  50. {$ifdef unused}
  51. procedure emitsymbolstabs(s : string;nidx,nother,line : longint;firstasm,secondasm : pasmsymbol);
  52. {$endif}
  53. procedure emitlineinfostabs(nidx,line : longint);
  54. procedure emitstabs(s:string);
  55. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  56. procedure StartFileLineInfo;
  57. {$endif}
  58. function MaybeNextList(var hp:pai):boolean;
  59. function TreePass0(hp:pai):pai;
  60. function TreePass1(hp:pai):pai;
  61. function TreePass2(hp:pai):pai;
  62. procedure writetree;
  63. procedure writetreesmart;
  64. end;
  65. implementation
  66. uses
  67. strings,
  68. globtype,globals,systems,verbose,
  69. cpuasm,
  70. {$ifdef GDB}
  71. gdb,
  72. {$endif}
  73. og386,og386dbg,og386cff;
  74. {$ifdef GDB}
  75. procedure ti386binasmlist.convertstabs(p:pchar);
  76. var
  77. ofs,
  78. nidx,nother,ii,i,line,j : longint;
  79. code : integer;
  80. hp : pchar;
  81. reloc : boolean;
  82. sec : tsection;
  83. ps : pasmsymbol;
  84. s : string;
  85. begin
  86. ofs:=0;
  87. reloc:=true;
  88. ps:=nil;
  89. sec:=sec_none;
  90. if p[0]='"' then
  91. begin
  92. i:=1;
  93. { we can have \" inside the string !! PM }
  94. while not ((p[i]='"') and (p[i-1]<>'\')) do
  95. inc(i);
  96. p[i]:=#0;
  97. ii:=i;
  98. hp:=@p[1];
  99. s:=StrPas(@P[i+2]);
  100. end
  101. else
  102. begin
  103. hp:=nil;
  104. s:=StrPas(P);
  105. i:=-2; {needed below (PM) }
  106. end;
  107. { When in pass 1 then only alloc and leave }
  108. if currpass=1 then
  109. begin
  110. objectalloc^.staballoc(hp);
  111. if assigned(hp) then
  112. p[i]:='"';
  113. exit;
  114. end;
  115. { Parse the rest of the stabs }
  116. if s='' then
  117. internalerror(33000);
  118. j:=pos(',',s);
  119. if j=0 then
  120. internalerror(33001);
  121. Val(Copy(s,1,j-1),nidx,code);
  122. if code<>0 then
  123. internalerror(33002);
  124. i:=i+2+j;
  125. Delete(s,1,j);
  126. j:=pos(',',s);
  127. if (j=0) then
  128. internalerror(33003);
  129. Val(Copy(s,1,j-1),nother,code);
  130. if code<>0 then
  131. internalerror(33004);
  132. i:=i+j;
  133. Delete(s,1,j);
  134. j:=pos(',',s);
  135. if j=0 then
  136. begin
  137. j:=256;
  138. ofs:=-1;
  139. end;
  140. Val(Copy(s,1,j-1),line,code);
  141. if code<>0 then
  142. internalerror(33005);
  143. if ofs=0 then
  144. begin
  145. Delete(s,1,j);
  146. i:=i+j;
  147. Val(s,ofs,code);
  148. if code=0 then
  149. reloc:=false
  150. else
  151. begin
  152. ofs:=0;
  153. s:=strpas(@p[i]);
  154. { handle asmsymbol or
  155. asmsymbol - asmsymbol }
  156. j:=pos(' ',s);
  157. if j=0 then
  158. j:=pos('-',s);
  159. { single asmsymbol }
  160. if j=0 then
  161. j:=256;
  162. ps:=getasmsymbol(copy(s,1,j-1));
  163. if not assigned(ps) then
  164. internalerror(33006)
  165. else
  166. begin
  167. sec:=ps^.section;
  168. ofs:=ps^.address;
  169. reloc:=true;
  170. end;
  171. if j<256 then
  172. begin
  173. i:=i+j;
  174. s:=strpas(@p[i]);
  175. if (s<>'') and (s[1]=' ') then
  176. begin
  177. j:=0;
  178. while (s[j+1]=' ') do
  179. inc(j);
  180. i:=i+j;
  181. s:=strpas(@p[i]);
  182. end;
  183. ps:=getasmsymbol(s);
  184. if not assigned(ps) then
  185. internalerror(33007)
  186. else
  187. begin
  188. if ps^.section<>sec then
  189. internalerror(33008);
  190. ofs:=ofs-ps^.address;
  191. reloc:=false;
  192. end;
  193. end;
  194. end;
  195. end;
  196. { external bss need speical handling (PM) }
  197. if assigned(ps) and (ps^.section=sec_none) then
  198. objectoutput^.WriteSymStabs(sec,ofs,hp,ps,nidx,nother,line,reloc)
  199. else
  200. objectoutput^.WriteStabs(sec,ofs,hp,nidx,nother,line,reloc);
  201. if assigned(hp) then
  202. p[ii]:='"';
  203. end;
  204. {$ifdef unused}
  205. procedure ti386binasmlist.emitsymbolstabs(s : string;nidx,nother,line : longint;
  206. firstasm,secondasm : pasmsymbol);
  207. var
  208. hp : pchar;
  209. begin
  210. if s='' then
  211. hp:=nil
  212. else
  213. begin
  214. s:=s+#0;
  215. hp:=@s[1];
  216. end;
  217. if not assigned(secondasm) then
  218. begin
  219. if not assigned(firstasm) then
  220. internalerror(33009);
  221. objectoutput^.WriteStabs(firstasm^.section,firstasm^.address,hp,nidx,nother,line,true);
  222. end
  223. else
  224. begin
  225. if firstasm^.section<>secondasm^.section then
  226. internalerror(33010);
  227. objectoutput^.WriteStabs(firstasm^.section,firstasm^.address-secondasm^.address,
  228. hp,nidx,nother,line,false);
  229. end;
  230. end;
  231. {$endif}
  232. procedure ti386binasmlist.emitlineinfostabs(nidx,line : longint);
  233. var
  234. sec : tsection;
  235. begin
  236. if currpass=1 then
  237. begin
  238. objectalloc^.staballoc(nil);
  239. exit;
  240. end;
  241. if (nidx=n_textline) and assigned(funcname) and
  242. (target_os.use_function_relative_addresses) then
  243. objectoutput^.WriteStabs(sec_code,pgenericcoffoutput(objectoutput)^.sects[sec_code]^.len-funcname^.address,
  244. nil,nidx,0,line,false)
  245. else
  246. begin
  247. if nidx=n_textline then
  248. sec:=sec_code
  249. else if nidx=n_dataline then
  250. sec:=sec_data
  251. else
  252. sec:=sec_bss;
  253. objectoutput^.WriteStabs(sec,pgenericcoffoutput(objectoutput)^.sects[sec]^.len,
  254. nil,nidx,0,line,true);
  255. end;
  256. end;
  257. procedure ti386binasmlist.emitstabs(s:string);
  258. begin
  259. s:=s+#0;
  260. ConvertStabs(@s[1]);
  261. end;
  262. procedure ti386binasmlist.WriteFileLineInfo(var fileinfo : tfileposinfo);
  263. var
  264. curr_n : byte;
  265. hp : pasmsymbol;
  266. infile : pinputfile;
  267. begin
  268. if not (cs_debuginfo in aktmoduleswitches) then
  269. exit;
  270. { file changed ? (must be before line info) }
  271. if (fileinfo.fileindex<>0) and
  272. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  273. begin
  274. infile:=current_module^.sourcefiles^.get_file(fileinfo.fileindex);
  275. if includecount=0 then
  276. curr_n:=n_sourcefile
  277. else
  278. curr_n:=n_includefile;
  279. { get symbol for this includefile }
  280. hp:=newasmsymbol('Ltext'+ToStr(IncludeCount));
  281. if currpass=1 then
  282. begin
  283. hp^.typ:=AS_LOCAL;
  284. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  285. end
  286. else
  287. objectoutput^.writesymbol(hp);
  288. { emit stabs }
  289. if (infile^.path^<>'') then
  290. EmitStabs('"'+lower(BsToSlash(FixPath(infile^.path^,false)))+'",'+tostr(curr_n)+
  291. ',0,0,Ltext'+ToStr(IncludeCount));
  292. EmitStabs('"'+lower(FixFileName(infile^.name^))+'",'+tostr(curr_n)+
  293. ',0,0,Ltext'+ToStr(IncludeCount));
  294. inc(includecount);
  295. end;
  296. { line changed ? }
  297. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  298. emitlineinfostabs(n_line,fileinfo.line);
  299. stabslastfileinfo:=fileinfo;
  300. end;
  301. procedure ti386binasmlist.StartFileLineInfo;
  302. var
  303. fileinfo : tfileposinfo;
  304. begin
  305. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  306. n_line:=n_textline;
  307. funcname:=nil;
  308. linecount:=1;
  309. includecount:=0;
  310. fileinfo.fileindex:=1;
  311. fileinfo.line:=1;
  312. WriteFileLineInfo(fileinfo);
  313. end;
  314. {$endif GDB}
  315. function ti386binasmlist.MaybeNextList(var hp:pai):boolean;
  316. begin
  317. { maybe end of list }
  318. while not assigned(hp) do
  319. begin
  320. if currlistidx<lists then
  321. begin
  322. inc(currlistidx);
  323. currlist:=list[currlistidx];
  324. hp:=pai(currlist^.first);
  325. end
  326. else
  327. begin
  328. MaybeNextList:=false;
  329. exit;
  330. end;
  331. end;
  332. MaybeNextList:=true;
  333. end;
  334. function ti386binasmlist.TreePass0(hp:pai):pai;
  335. var
  336. lastsec : tsection;
  337. l : longint;
  338. begin
  339. while assigned(hp) do
  340. begin
  341. case hp^.typ of
  342. ait_align :
  343. begin
  344. { always use the maximum fillsize in this pass to avoid possible
  345. short jumps to become out of range }
  346. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype;
  347. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  348. end;
  349. ait_datablock :
  350. begin
  351. {$ifdef EXTERNALBSS}
  352. if not SmartAsm then
  353. begin
  354. if not pai_datablock(hp)^.is_global then
  355. begin
  356. l:=pai_datablock(hp)^.size;
  357. if l>2 then
  358. objectalloc^.sectionalign(4)
  359. else if l>1 then
  360. objectalloc^.sectionalign(2);
  361. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  362. end;
  363. end
  364. else
  365. begin
  366. {$endif}
  367. l:=pai_datablock(hp)^.size;
  368. if l>2 then
  369. objectalloc^.sectionalign(4)
  370. else if l>1 then
  371. objectalloc^.sectionalign(2);
  372. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  373. end;
  374. end;
  375. ait_const_32bit :
  376. objectalloc^.sectionalloc(4);
  377. ait_const_16bit :
  378. objectalloc^.sectionalloc(2);
  379. ait_const_8bit :
  380. objectalloc^.sectionalloc(1);
  381. ait_real_80bit :
  382. objectalloc^.sectionalloc(10);
  383. ait_real_64bit :
  384. objectalloc^.sectionalloc(8);
  385. ait_real_32bit :
  386. objectalloc^.sectionalloc(4);
  387. ait_comp_64bit :
  388. objectalloc^.sectionalloc(8);
  389. ait_const_rva,
  390. ait_const_symbol :
  391. objectalloc^.sectionalloc(4);
  392. ait_section:
  393. begin
  394. objectalloc^.setsection(pai_section(hp)^.sec);
  395. lastsec:=pai_section(hp)^.sec;
  396. end;
  397. ait_symbol :
  398. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  399. ait_label :
  400. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  401. ait_string :
  402. objectalloc^.sectionalloc(pai_string(hp)^.len);
  403. ait_instruction :
  404. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  405. ait_cut :
  406. if SmartAsm then
  407. begin
  408. objectalloc^.resetsections;
  409. objectalloc^.setsection(lastsec);
  410. end;
  411. end;
  412. hp:=pai(hp^.next);
  413. end;
  414. TreePass0:=hp;
  415. end;
  416. function ti386binasmlist.TreePass1(hp:pai):pai;
  417. var
  418. l : longint;
  419. begin
  420. while assigned(hp) do
  421. begin
  422. {$ifdef GDB}
  423. { write stabs }
  424. if (cs_debuginfo in aktmoduleswitches) then
  425. begin
  426. if (objectalloc^.currsec<>sec_none) and
  427. not(hp^.typ in [
  428. ait_label,
  429. ait_regalloc,ait_tempalloc,
  430. ait_stabn,ait_stabs,ait_section,
  431. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  432. WriteFileLineInfo(hp^.fileinfo);
  433. end;
  434. {$endif GDB}
  435. case hp^.typ of
  436. ait_align :
  437. begin
  438. { always use the maximum fillsize in this pass to avoid possible
  439. short jumps to become out of range }
  440. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype;
  441. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  442. end;
  443. ait_datablock :
  444. begin
  445. if objectalloc^.currsec<>sec_bss then
  446. Message(asmw_e_alloc_data_only_in_bss);
  447. {$ifdef EXTERNALBSS}
  448. if not SmartAsm then
  449. begin
  450. if pai_datablock(hp)^.is_global then
  451. begin
  452. pai_datablock(hp)^.sym^.typ:=AS_EXTERNAL;
  453. pai_datablock(hp)^.sym^.setaddress(sec_none,pai_datablock(hp)^.size,pai_datablock(hp)^.size);
  454. end
  455. else
  456. begin
  457. l:=pai_datablock(hp)^.size;
  458. if l>2 then
  459. objectalloc^.sectionalign(4)
  460. else if l>1 then
  461. objectalloc^.sectionalign(2);
  462. pai_datablock(hp)^.sym^.typ:=AS_LOCAL;
  463. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,
  464. pai_datablock(hp)^.size);
  465. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  466. end;
  467. end
  468. else
  469. {$endif}
  470. begin
  471. if pai_datablock(hp)^.is_global then
  472. pai_datablock(hp)^.sym^.typ:=AS_GLOBAL
  473. else
  474. pai_datablock(hp)^.sym^.typ:=AS_LOCAL;
  475. l:=pai_datablock(hp)^.size;
  476. if l>2 then
  477. objectalloc^.sectionalign(4)
  478. else if l>1 then
  479. objectalloc^.sectionalign(2);
  480. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  481. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  482. end;
  483. end;
  484. ait_const_32bit :
  485. objectalloc^.sectionalloc(4);
  486. ait_const_16bit :
  487. objectalloc^.sectionalloc(2);
  488. ait_const_8bit :
  489. objectalloc^.sectionalloc(1);
  490. ait_real_80bit :
  491. objectalloc^.sectionalloc(10);
  492. ait_real_64bit :
  493. objectalloc^.sectionalloc(8);
  494. ait_real_32bit :
  495. objectalloc^.sectionalloc(4);
  496. ait_comp_64bit :
  497. objectalloc^.sectionalloc(8);
  498. ait_const_rva,
  499. ait_const_symbol :
  500. objectalloc^.sectionalloc(4);
  501. ait_section:
  502. begin
  503. objectalloc^.setsection(pai_section(hp)^.sec);
  504. {$ifdef GDB}
  505. case pai_section(hp)^.sec of
  506. sec_code : n_line:=n_textline;
  507. sec_data : n_line:=n_dataline;
  508. sec_bss : n_line:=n_bssline;
  509. else
  510. n_line:=n_dataline;
  511. end;
  512. stabslastfileinfo.line:=-1;
  513. {$endif GDB}
  514. end;
  515. {$ifdef GDB}
  516. ait_stabn :
  517. convertstabs(pai_stabn(hp)^.str);
  518. ait_stabs :
  519. convertstabs(pai_stabs(hp)^.str);
  520. ait_stab_function_name :
  521. if assigned(pai_stab_function_name(hp)^.str) then
  522. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  523. else
  524. funcname:=nil;
  525. ait_force_line :
  526. stabslastfileinfo.line:=0;
  527. {$endif}
  528. ait_symbol :
  529. begin
  530. if pai_symbol(hp)^.is_global then
  531. pai_symbol(hp)^.sym^.typ:=AS_GLOBAL
  532. else
  533. pai_symbol(hp)^.sym^.typ:=AS_LOCAL;
  534. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  535. end;
  536. ait_label :
  537. begin
  538. if pai_label(hp)^.is_global then
  539. pai_label(hp)^.l^.typ:=AS_GLOBAL
  540. else
  541. pai_label(hp)^.l^.typ:=AS_LOCAL;
  542. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  543. end;
  544. ait_string :
  545. objectalloc^.sectionalloc(pai_string(hp)^.len);
  546. ait_instruction :
  547. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  548. ait_direct :
  549. Message(asmw_f_direct_not_supported);
  550. ait_cut :
  551. if SmartAsm then
  552. break;
  553. end;
  554. hp:=pai(hp^.next);
  555. end;
  556. TreePass1:=hp;
  557. end;
  558. function ti386binasmlist.TreePass2(hp:pai):pai;
  559. const
  560. alignarray:array[0..5] of string[8]=(
  561. #$8D#$B4#$26#$00#$00#$00#$00,
  562. #$8D#$B6#$00#$00#$00#$00,
  563. #$8D#$74#$26#$00,
  564. #$8D#$76#$00,
  565. #$89#$F6,
  566. #$90
  567. );
  568. var
  569. l,j : longint;
  570. {$ifdef I386}
  571. co : comp;
  572. {$endif I386}
  573. begin
  574. { main loop }
  575. while assigned(hp) do
  576. begin
  577. {$ifdef GDB}
  578. { write stabs }
  579. if cs_debuginfo in aktmoduleswitches then
  580. begin
  581. if (objectoutput^.currsec<>sec_none) and
  582. not(hp^.typ in [
  583. ait_label,
  584. ait_regalloc,ait_tempalloc,
  585. ait_stabn,ait_stabs,ait_section,
  586. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  587. WriteFileLineInfo(hp^.fileinfo);
  588. end;
  589. {$endif GDB}
  590. case hp^.typ of
  591. ait_align :
  592. begin
  593. l:=pai_align(hp)^.fillsize;
  594. while (l>0) do
  595. begin
  596. for j:=0to 5 do
  597. if (l>=length(alignarray[j])) then
  598. break;
  599. objectoutput^.writebytes(alignarray[j][1],length(alignarray[j]));
  600. dec(l,length(alignarray[j]));
  601. end;
  602. end;
  603. ait_section :
  604. begin
  605. objectoutput^.defaultsection(pai_section(hp)^.sec);
  606. {$ifdef GDB}
  607. case pai_section(hp)^.sec of
  608. sec_code : n_line:=n_textline;
  609. sec_data : n_line:=n_dataline;
  610. sec_bss : n_line:=n_bssline;
  611. else
  612. n_line:=n_dataline;
  613. end;
  614. stabslastfileinfo.line:=-1;
  615. {$endif GDB}
  616. end;
  617. ait_symbol :
  618. objectoutput^.writesymbol(pai_symbol(hp)^.sym);
  619. ait_datablock :
  620. begin
  621. objectoutput^.writesymbol(pai_datablock(hp)^.sym);
  622. if SmartAsm
  623. {$ifdef EXTERNALBSS}
  624. or (not pai_datablock(hp)^.is_global)
  625. {$endif}
  626. then
  627. begin
  628. l:=pai_datablock(hp)^.size;
  629. if l>2 then
  630. objectoutput^.writealign(4)
  631. else if l>1 then
  632. objectoutput^.writealign(2);
  633. objectoutput^.writealloc(pai_datablock(hp)^.size);
  634. end;
  635. end;
  636. ait_const_32bit :
  637. objectoutput^.writebytes(pai_const(hp)^.value,4);
  638. ait_const_16bit :
  639. objectoutput^.writebytes(pai_const(hp)^.value,2);
  640. ait_const_8bit :
  641. objectoutput^.writebytes(pai_const(hp)^.value,1);
  642. ait_real_80bit :
  643. objectoutput^.writebytes(pai_real_80bit(hp)^.value,10);
  644. ait_real_64bit :
  645. objectoutput^.writebytes(pai_real_64bit(hp)^.value,8);
  646. ait_real_32bit :
  647. objectoutput^.writebytes(pai_real_32bit(hp)^.value,4);
  648. ait_comp_64bit :
  649. begin
  650. {$ifdef FPC}
  651. co:=comp(pai_comp_64bit(hp)^.value);
  652. {$else}
  653. co:=pai_comp_64bit(hp)^.value;
  654. {$endif}
  655. objectoutput^.writebytes(co,8);
  656. end;
  657. ait_string :
  658. objectoutput^.writebytes(pai_string(hp)^.str^,pai_string(hp)^.len);
  659. ait_const_rva :
  660. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  661. pai_const_symbol(hp)^.sym,relative_rva);
  662. ait_const_symbol :
  663. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  664. pai_const_symbol(hp)^.sym,relative_false);
  665. ait_label :
  666. objectoutput^.writesymbol(pai_label(hp)^.l);
  667. ait_instruction :
  668. paicpu(hp)^.Pass2;
  669. {$ifdef GDB}
  670. ait_stabn :
  671. convertstabs(pai_stabn(hp)^.str);
  672. ait_stabs :
  673. convertstabs(pai_stabs(hp)^.str);
  674. ait_stab_function_name :
  675. if assigned(pai_stab_function_name(hp)^.str) then
  676. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  677. else
  678. funcname:=nil;
  679. ait_force_line :
  680. stabslastfileinfo.line:=0;
  681. {$endif}
  682. ait_cut :
  683. if SmartAsm then
  684. break;
  685. end;
  686. hp:=pai(hp^.next);
  687. end;
  688. TreePass2:=hp;
  689. end;
  690. procedure ti386binasmlist.writetree;
  691. var
  692. hp : pai;
  693. begin
  694. objectalloc^.setsection(sec_code);
  695. objectoutput^.defaultsection(sec_code);
  696. { Pass 1 }
  697. currpass:=1;
  698. {$ifdef GDB}
  699. StartFileLineInfo;
  700. {$endif GDB}
  701. { start with list 1 }
  702. currlistidx:=1;
  703. currlist:=list[currlistidx];
  704. hp:=pai(currlist^.first);
  705. while assigned(hp) do
  706. begin
  707. hp:=TreePass1(hp);
  708. MaybeNextList(hp);
  709. end;
  710. { set section sizes }
  711. objectoutput^.setsectionsizes(objectalloc^.secsize);
  712. { Pass 2 }
  713. currpass:=2;
  714. {$ifdef GDB}
  715. StartFileLineInfo;
  716. {$endif GDB}
  717. { start with list 1 }
  718. currlistidx:=1;
  719. currlist:=list[currlistidx];
  720. hp:=pai(currlist^.first);
  721. while assigned(hp) do
  722. begin
  723. hp:=TreePass2(hp);
  724. MaybeNextList(hp);
  725. end;
  726. end;
  727. procedure ti386binasmlist.writetreesmart;
  728. var
  729. hp,hp1 : pai;
  730. begin
  731. objectalloc^.setsection(sec_code);
  732. objectoutput^.defaultsection(sec_code);
  733. { start with list 1 }
  734. currlistidx:=1;
  735. currlist:=list[currlistidx];
  736. hp:=pai(currlist^.first);
  737. while assigned(hp) do
  738. begin
  739. { Pass 1 }
  740. currpass:=1;
  741. {$ifdef GDB}
  742. StartFileLineInfo;
  743. {$endif GDB}
  744. hp1:=TreePass1(hp);
  745. { set section sizes }
  746. objectoutput^.setsectionsizes(objectalloc^.secsize);
  747. { Pass 2 }
  748. currpass:=2;
  749. {$ifdef GDB}
  750. StartFileLineInfo;
  751. {$endif GDB}
  752. hp:=TreePass2(hp);
  753. if not MaybeNextList(hp) then
  754. break;
  755. { write the current objectfile }
  756. objectoutput^.donewriting;
  757. { we will start a new objectfile so reset everything }
  758. objectoutput^.initwriting;
  759. objectalloc^.resetsections;
  760. ResetAsmsymbolList;
  761. { avoid empty files }
  762. while assigned(hp^.next) and
  763. (pai(hp^.next)^.typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  764. begin
  765. if pai(hp^.next)^.typ=ait_section then
  766. begin
  767. objectalloc^.setsection(pai_section(hp^.next)^.sec);
  768. objectoutput^.defaultsection(pai_section(hp^.next)^.sec);
  769. end;
  770. hp:=pai(hp^.next);
  771. end;
  772. hp:=pai(hp^.next);
  773. if not MaybeNextList(hp) then
  774. break;
  775. end;
  776. end;
  777. procedure ti386binasmlist.writebin;
  778. procedure addlist(p:paasmoutput);
  779. begin
  780. inc(lists);
  781. list[lists]:=p;
  782. end;
  783. begin
  784. {$ifdef MULTIPASS}
  785. { Process the codesegment twice so the short jmp instructions can
  786. be optimized }
  787. currpass:=0;
  788. TreePass0(pai(codesegment^.first));
  789. {$endif}
  790. objectalloc^.resetsections;
  791. objectalloc^.setsection(sec_code);
  792. objectoutput^.initwriting;
  793. objectoutput^.defaultsection(sec_code);
  794. if cs_debuginfo in aktmoduleswitches then
  795. addlist(debuglist);
  796. addlist(codesegment);
  797. addlist(datasegment);
  798. addlist(consts);
  799. addlist(rttilist);
  800. if assigned(resourcestringlist) then
  801. addlist(resourcestringlist);
  802. addlist(bsssegment);
  803. if assigned(importssection) then
  804. addlist(importssection);
  805. if assigned(exportssection) and not RelocSection then
  806. addlist(exportssection);
  807. if assigned(resourcesection) then
  808. addlist(resourcesection);
  809. if SmartAsm then
  810. writetreesmart
  811. else
  812. writetree;
  813. objectoutput^.donewriting;
  814. end;
  815. constructor ti386binasmlist.init(t:togtype;smart:boolean);
  816. begin
  817. case t of
  818. og_none :
  819. Message(asmw_f_no_binary_writer_selected);
  820. og_dbg :
  821. objectoutput:=new(pdbgoutput,init(smart));
  822. og_coff :
  823. objectoutput:=new(pdjgppcoffoutput,init(smart));
  824. og_pecoff :
  825. objectoutput:=new(pwin32coffoutput,init(smart));
  826. end;
  827. objectalloc:=new(pobjectalloc,init);
  828. SmartAsm:=smart;
  829. currpass:=0;
  830. end;
  831. destructor ti386binasmlist.done;
  832. begin
  833. dispose(objectoutput,done);
  834. dispose(objectalloc,done);
  835. end;
  836. end.
  837. {
  838. $Log$
  839. Revision 1.25 1999-09-26 21:13:40 peter
  840. * short jmp with alignment problems fixed
  841. Revision 1.24 1999/08/25 11:59:33 jonas
  842. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  843. Revision 1.23 1999/08/10 12:26:21 pierre
  844. * avoid double .edata section if using DLLTOOL
  845. Revision 1.22 1999/08/04 00:22:35 florian
  846. * renamed i386asm and i386base to cpuasm and cpubase
  847. Revision 1.21 1999/08/01 18:28:09 florian
  848. * modifications for the new code generator
  849. Revision 1.20 1999/07/31 12:33:11 peter
  850. * fixed smartlinking
  851. Revision 1.19 1999/07/22 09:37:30 florian
  852. + resourcestring implemented
  853. + start of longstring support
  854. Revision 1.18 1999/07/03 00:26:02 peter
  855. * ag386bin doesn't destroy the aasmoutput lists anymore
  856. Revision 1.17 1999/06/10 23:52:34 pierre
  857. * merged from fixes branch
  858. Revision 1.16.2.1 1999/06/10 23:33:35 pierre
  859. * pchar memory loss and .bss size problem solved
  860. Revision 1.16 1999/06/03 16:39:10 pierre
  861. * EXTERNALBSS fixed for stabs and default again
  862. Revision 1.15 1999/06/02 22:43:59 pierre
  863. * previous wrong log corrected
  864. Revision 1.14 1999/06/02 22:25:25 pierre
  865. * changed $ifdef FPC @ into $ifndef TP
  866. Revision 1.13 1999/06/01 10:24:09 pierre
  867. * ts010021.pp problem solved for ag386bin !
  868. Revision 1.12 1999/05/27 19:43:59 peter
  869. * removed oldasm
  870. * plabel -> pasmlabel
  871. * -a switches to source writing automaticly
  872. * assembler readers OOPed
  873. * asmsymbol automaticly external
  874. * jumptables and other label fixes for asm readers
  875. Revision 1.11 1999/05/21 13:54:41 peter
  876. * NEWLAB for label as symbol
  877. Revision 1.10 1999/05/19 11:54:17 pierre
  878. + experimental code for externalbss and stabs problem
  879. Revision 1.9 1999/05/12 00:19:37 peter
  880. * removed R_DEFAULT_SEG
  881. * uniform float names
  882. Revision 1.8 1999/05/09 11:38:04 peter
  883. * don't write .o and link if errors occure during assembling
  884. Revision 1.6 1999/05/07 00:36:58 pierre
  885. * added alignment code for .bss
  886. * stabs correct but externalbss disabled
  887. would need a special treatment in writestabs
  888. Revision 1.5 1999/05/06 09:05:07 peter
  889. * generic write_float and str_float
  890. * fixed constant float conversions
  891. Revision 1.4 1999/05/05 22:21:47 peter
  892. * updated messages
  893. Revision 1.3 1999/05/05 17:34:29 peter
  894. * output is more like as 2.9.1
  895. * stabs really working for go32v2
  896. Revision 1.2 1999/05/04 21:44:30 florian
  897. * changes to compile it with Delphi 4.0
  898. Revision 1.1 1999/05/01 13:23:57 peter
  899. * merged nasm compiler
  900. * old asm moved to oldasm/
  901. Revision 1.14 1999/04/16 11:49:48 peter
  902. + tempalloc
  903. + -at to show temp alloc info in .s file
  904. Revision 1.13 1999/03/12 00:20:03 pierre
  905. + win32 output working !
  906. Revision 1.12 1999/03/11 17:52:34 peter
  907. * fixed wrong ot_signed generation in insns tab
  908. Revision 1.11 1999/03/10 13:41:07 pierre
  909. + partial implementation for win32 !
  910. winhello works but pp still does not !
  911. Revision 1.10 1999/03/08 14:51:05 peter
  912. + smartlinking for ag386bin
  913. Revision 1.9 1999/03/06 17:24:18 peter
  914. * rewritten intel parser a lot, especially reference reading
  915. * size checking added for asm parsers
  916. Revision 1.8 1999/03/05 13:09:50 peter
  917. * first things for tai_cut support for ag386bin
  918. Revision 1.7 1999/03/03 11:41:53 pierre
  919. + stabs info corrected to give results near to GAS output
  920. * local labels (with .L are not stored in object anymore)
  921. so we get the same number of symbols as from GAS !
  922. Revision 1.6 1999/03/03 01:36:44 pierre
  923. + stabs output working (though not really tested)
  924. for a simple file the only difference to GAS output is due
  925. to the VMA of the different sections
  926. Revision 1.5 1999/03/02 02:56:18 peter
  927. + stabs support for binary writers
  928. * more fixes and missing updates from the previous commit :(
  929. Revision 1.4 1999/03/01 15:46:20 peter
  930. * ag386bin finally make cycles correct
  931. * prefixes are now also normal opcodes
  932. Revision 1.3 1999/02/25 21:03:01 peter
  933. * ag386bin updates
  934. + coff writer
  935. Revision 1.2 1999/02/22 02:16:00 peter
  936. * updates for ag386bin
  937. Revision 1.1 1999/02/16 17:59:37 peter
  938. + initial files
  939. }