ag386bin.pas 32 KB

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