ag386bin.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. { here we must determine the fillsize which is used in pass2 }
  439. pai_align(hp)^.fillsize:=align(objectalloc^.sectionsize,pai_align(hp)^.aligntype)-
  440. objectalloc^.sectionsize;
  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. alignoparray:array[0..63] of byte;
  571. {$ifdef I386}
  572. co : comp;
  573. {$endif I386}
  574. begin
  575. { main loop }
  576. while assigned(hp) do
  577. begin
  578. {$ifdef GDB}
  579. { write stabs }
  580. if cs_debuginfo in aktmoduleswitches then
  581. begin
  582. if (objectoutput^.currsec<>sec_none) and
  583. not(hp^.typ in [
  584. ait_label,
  585. ait_regalloc,ait_tempalloc,
  586. ait_stabn,ait_stabs,ait_section,
  587. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  588. WriteFileLineInfo(hp^.fileinfo);
  589. end;
  590. {$endif GDB}
  591. case hp^.typ of
  592. ait_align :
  593. begin
  594. if not pai_align(hp)^.use_op then
  595. begin
  596. l:=pai_align(hp)^.fillsize;
  597. while (l>0) do
  598. begin
  599. for j:=0to 5 do
  600. if (l>=length(alignarray[j])) then
  601. break;
  602. objectoutput^.writebytes(alignarray[j][1],length(alignarray[j]));
  603. dec(l,length(alignarray[j]));
  604. end;
  605. end
  606. else
  607. begin
  608. fillchar(alignoparray,pai_align(hp)^.fillsize,pai_align(hp)^.fillop);
  609. objectoutput^.writebytes(alignoparray,pai_align(hp)^.fillsize);
  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. paicpu(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 : 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. 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. if (hp^.typ=ait_cut) then
  768. objectoutput^.initwriting(pai_cut(hp)^.place)
  769. else
  770. objectoutput^.initwriting(cut_normal);
  771. objectalloc^.resetsections;
  772. ResetAsmsymbolList;
  773. { avoid empty files }
  774. while assigned(hp^.next) and
  775. (pai(hp^.next)^.typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  776. begin
  777. if pai(hp^.next)^.typ=ait_section then
  778. begin
  779. objectalloc^.setsection(pai_section(hp^.next)^.sec);
  780. objectoutput^.defaultsection(pai_section(hp^.next)^.sec);
  781. end;
  782. hp:=pai(hp^.next);
  783. end;
  784. hp:=pai(hp^.next);
  785. if not MaybeNextList(hp) then
  786. break;
  787. end;
  788. end;
  789. procedure ti386binasmlist.writebin;
  790. procedure addlist(p:paasmoutput);
  791. begin
  792. inc(lists);
  793. list[lists]:=p;
  794. end;
  795. begin
  796. {$ifdef MULTIPASS}
  797. { Process the codesegment twice so the short jmp instructions can
  798. be optimized }
  799. currpass:=0;
  800. TreePass0(pai(codesegment^.first));
  801. {$endif}
  802. objectalloc^.resetsections;
  803. objectalloc^.setsection(sec_code);
  804. objectoutput^.initwriting(cut_normal);
  805. objectoutput^.defaultsection(sec_code);
  806. if cs_debuginfo in aktmoduleswitches then
  807. addlist(debuglist);
  808. addlist(codesegment);
  809. addlist(datasegment);
  810. addlist(consts);
  811. addlist(rttilist);
  812. if assigned(resourcestringlist) then
  813. addlist(resourcestringlist);
  814. addlist(bsssegment);
  815. if assigned(importssection) then
  816. addlist(importssection);
  817. if assigned(exportssection) and not RelocSection then
  818. addlist(exportssection);
  819. if assigned(resourcesection) then
  820. addlist(resourcesection);
  821. if SmartAsm then
  822. writetreesmart
  823. else
  824. writetree;
  825. objectoutput^.donewriting;
  826. end;
  827. constructor ti386binasmlist.init(t:togtype;smart:boolean);
  828. begin
  829. case t of
  830. og_none :
  831. Message(asmw_f_no_binary_writer_selected);
  832. og_dbg :
  833. objectoutput:=new(pdbgoutput,init(smart));
  834. og_coff :
  835. objectoutput:=new(pdjgppcoffoutput,init(smart));
  836. og_pecoff :
  837. objectoutput:=new(pwin32coffoutput,init(smart));
  838. end;
  839. objectalloc:=new(pobjectalloc,init);
  840. SmartAsm:=smart;
  841. currpass:=0;
  842. end;
  843. destructor ti386binasmlist.done;
  844. begin
  845. dispose(objectoutput,done);
  846. dispose(objectalloc,done);
  847. end;
  848. end.
  849. {
  850. $Log$
  851. Revision 1.28 1999-11-30 10:40:42 peter
  852. + ttype, tsymlist
  853. Revision 1.27 1999/11/06 14:34:16 peter
  854. * truncated log to 20 revs
  855. Revision 1.26 1999/11/02 15:06:56 peter
  856. * import library fixes for win32
  857. * alignment works again
  858. Revision 1.25 1999/09/26 21:13:40 peter
  859. * short jmp with alignment problems fixed
  860. Revision 1.24 1999/08/25 11:59:33 jonas
  861. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  862. Revision 1.23 1999/08/10 12:26:21 pierre
  863. * avoid double .edata section if using DLLTOOL
  864. Revision 1.22 1999/08/04 00:22:35 florian
  865. * renamed i386asm and i386base to cpuasm and cpubase
  866. Revision 1.21 1999/08/01 18:28:09 florian
  867. * modifications for the new code generator
  868. Revision 1.20 1999/07/31 12:33:11 peter
  869. * fixed smartlinking
  870. Revision 1.19 1999/07/22 09:37:30 florian
  871. + resourcestring implemented
  872. + start of longstring support
  873. Revision 1.18 1999/07/03 00:26:02 peter
  874. * ag386bin doesn't destroy the aasmoutput lists anymore
  875. Revision 1.17 1999/06/10 23:52:34 pierre
  876. * merged from fixes branch
  877. Revision 1.16.2.1 1999/06/10 23:33:35 pierre
  878. * pchar memory loss and .bss size problem solved
  879. Revision 1.16 1999/06/03 16:39:10 pierre
  880. * EXTERNALBSS fixed for stabs and default again
  881. Revision 1.15 1999/06/02 22:43:59 pierre
  882. * previous wrong log corrected
  883. Revision 1.14 1999/06/02 22:25:25 pierre
  884. * changed $ifdef FPC @ into $ifndef TP
  885. Revision 1.13 1999/06/01 10:24:09 pierre
  886. * ts010021.pp problem solved for ag386bin !
  887. Revision 1.12 1999/05/27 19:43:59 peter
  888. * removed oldasm
  889. * plabel -> pasmlabel
  890. * -a switches to source writing automaticly
  891. * assembler readers OOPed
  892. * asmsymbol automaticly external
  893. * jumptables and other label fixes for asm readers
  894. Revision 1.11 1999/05/21 13:54:41 peter
  895. * NEWLAB for label as symbol
  896. Revision 1.10 1999/05/19 11:54:17 pierre
  897. + experimental code for externalbss and stabs problem
  898. Revision 1.9 1999/05/12 00:19:37 peter
  899. * removed R_DEFAULT_SEG
  900. * uniform float names
  901. Revision 1.8 1999/05/09 11:38:04 peter
  902. * don't write .o and link if errors occure during assembling
  903. }