ag386bin.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 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. i386asm,
  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.TreePass0(hp:pai):pai;
  316. var
  317. lastsec : tsection;
  318. l : longint;
  319. begin
  320. while assigned(hp) do
  321. begin
  322. case hp^.typ of
  323. ait_align :
  324. begin
  325. if (objectalloc^.sectionsize mod pai_align(hp)^.aligntype)<>0 then
  326. begin
  327. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype-
  328. (objectalloc^.sectionsize mod pai_align(hp)^.aligntype);
  329. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  330. end
  331. else
  332. pai_align(hp)^.fillsize:=0;
  333. end;
  334. ait_datablock :
  335. begin
  336. {$ifdef EXTERNALBSS}
  337. if not pai_datablock(hp)^.is_global then
  338. begin
  339. l:=pai_datablock(hp)^.size;
  340. if l>2 then
  341. objectalloc^.sectionalign(4)
  342. else if l>1 then
  343. objectalloc^.sectionalign(2);
  344. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  345. end;
  346. {$else}
  347. l:=pai_datablock(hp)^.size;
  348. if l>2 then
  349. objectalloc^.sectionalign(4)
  350. else if l>1 then
  351. objectalloc^.sectionalign(2);
  352. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  353. {$endif}
  354. end;
  355. ait_const_32bit :
  356. objectalloc^.sectionalloc(4);
  357. ait_const_16bit :
  358. objectalloc^.sectionalloc(2);
  359. ait_const_8bit :
  360. objectalloc^.sectionalloc(1);
  361. ait_real_80bit :
  362. objectalloc^.sectionalloc(10);
  363. ait_real_64bit :
  364. objectalloc^.sectionalloc(8);
  365. ait_real_32bit :
  366. objectalloc^.sectionalloc(4);
  367. ait_comp_64bit :
  368. objectalloc^.sectionalloc(8);
  369. ait_const_rva,
  370. ait_const_symbol :
  371. objectalloc^.sectionalloc(4);
  372. ait_section:
  373. begin
  374. objectalloc^.setsection(pai_section(hp)^.sec);
  375. lastsec:=pai_section(hp)^.sec;
  376. end;
  377. ait_symbol :
  378. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  379. ait_label :
  380. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  381. ait_string :
  382. objectalloc^.sectionalloc(pai_string(hp)^.len);
  383. ait_instruction :
  384. objectalloc^.sectionalloc(pai386(hp)^.Pass1(objectalloc^.sectionsize));
  385. ait_cut :
  386. if SmartAsm then
  387. begin
  388. objectalloc^.resetsections;
  389. objectalloc^.setsection(lastsec);
  390. end;
  391. end;
  392. hp:=pai(hp^.next);
  393. { maybe end of list }
  394. if not assigned(hp) then
  395. begin
  396. if currlistidx<lists then
  397. begin
  398. inc(currlistidx);
  399. currlist:=list[currlistidx];
  400. hp:=pai(currlist^.first);
  401. end;
  402. end;
  403. end;
  404. TreePass0:=hp;
  405. end;
  406. function ti386binasmlist.TreePass1(hp:pai):pai;
  407. var
  408. l : longint;
  409. begin
  410. while assigned(hp) do
  411. begin
  412. {$ifdef GDB}
  413. { write stabs }
  414. if (cs_debuginfo in aktmoduleswitches) then
  415. begin
  416. if (objectalloc^.currsec<>sec_none) and
  417. not(hp^.typ in [
  418. ait_label,
  419. ait_regalloc,ait_tempalloc,
  420. ait_stabn,ait_stabs,ait_section,
  421. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  422. WriteFileLineInfo(hp^.fileinfo);
  423. end;
  424. {$endif GDB}
  425. case hp^.typ of
  426. ait_align :
  427. begin
  428. if (objectalloc^.sectionsize mod pai_align(hp)^.aligntype)<>0 then
  429. begin
  430. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype-
  431. (objectalloc^.sectionsize mod pai_align(hp)^.aligntype);
  432. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  433. end
  434. else
  435. pai_align(hp)^.fillsize:=0;
  436. end;
  437. ait_datablock :
  438. begin
  439. if objectalloc^.currsec<>sec_bss then
  440. Message(asmw_e_alloc_data_only_in_bss);
  441. {$ifdef EXTERNALBSS}
  442. if pai_datablock(hp)^.is_global then
  443. begin
  444. pai_datablock(hp)^.sym^.typ:=AS_EXTERNAL;
  445. pai_datablock(hp)^.sym^.setaddress(sec_none,pai_datablock(hp)^.size,pai_datablock(hp)^.size);
  446. end
  447. else
  448. begin
  449. l:=pai_datablock(hp)^.size;
  450. if l>2 then
  451. objectalloc^.sectionalign(4)
  452. else if l>1 then
  453. objectalloc^.sectionalign(2);
  454. pai_datablock(hp)^.sym^.typ:=AS_LOCAL;
  455. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  456. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  457. end;
  458. {$else}
  459. if pai_datablock(hp)^.is_global then
  460. pai_datablock(hp)^.sym^.typ:=AS_GLOBAL
  461. else
  462. pai_datablock(hp)^.sym^.typ:=AS_LOCAL;
  463. l:=pai_datablock(hp)^.size;
  464. if l>2 then
  465. objectalloc^.sectionalign(4)
  466. else if l>1 then
  467. objectalloc^.sectionalign(2);
  468. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  469. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  470. {$endif}
  471. end;
  472. ait_const_32bit :
  473. objectalloc^.sectionalloc(4);
  474. ait_const_16bit :
  475. objectalloc^.sectionalloc(2);
  476. ait_const_8bit :
  477. objectalloc^.sectionalloc(1);
  478. ait_real_80bit :
  479. objectalloc^.sectionalloc(10);
  480. ait_real_64bit :
  481. objectalloc^.sectionalloc(8);
  482. ait_real_32bit :
  483. objectalloc^.sectionalloc(4);
  484. ait_comp_64bit :
  485. objectalloc^.sectionalloc(8);
  486. ait_const_rva,
  487. ait_const_symbol :
  488. objectalloc^.sectionalloc(4);
  489. ait_section:
  490. begin
  491. objectalloc^.setsection(pai_section(hp)^.sec);
  492. {$ifdef GDB}
  493. case pai_section(hp)^.sec of
  494. sec_code : n_line:=n_textline;
  495. sec_data : n_line:=n_dataline;
  496. sec_bss : n_line:=n_bssline;
  497. else
  498. n_line:=n_dataline;
  499. end;
  500. stabslastfileinfo.line:=-1;
  501. {$endif GDB}
  502. end;
  503. {$ifdef GDB}
  504. ait_stabn :
  505. convertstabs(pai_stabn(hp)^.str);
  506. ait_stabs :
  507. convertstabs(pai_stabs(hp)^.str);
  508. ait_stab_function_name :
  509. if assigned(pai_stab_function_name(hp)^.str) then
  510. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  511. else
  512. funcname:=nil;
  513. ait_force_line :
  514. stabslastfileinfo.line:=0;
  515. {$endif}
  516. ait_symbol :
  517. begin
  518. if pai_symbol(hp)^.is_global then
  519. pai_symbol(hp)^.sym^.typ:=AS_GLOBAL
  520. else
  521. pai_symbol(hp)^.sym^.typ:=AS_LOCAL;
  522. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  523. end;
  524. ait_label :
  525. begin
  526. if pai_label(hp)^.is_global then
  527. pai_label(hp)^.l^.typ:=AS_GLOBAL
  528. else
  529. pai_label(hp)^.l^.typ:=AS_LOCAL;
  530. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  531. end;
  532. ait_string :
  533. objectalloc^.sectionalloc(pai_string(hp)^.len);
  534. ait_instruction :
  535. objectalloc^.sectionalloc(pai386(hp)^.Pass1(objectalloc^.sectionsize));
  536. ait_direct :
  537. Message(asmw_f_direct_not_supported);
  538. ait_cut :
  539. if SmartAsm then
  540. break;
  541. end;
  542. hp:=pai(hp^.next);
  543. end;
  544. TreePass1:=hp;
  545. end;
  546. function ti386binasmlist.TreePass2(hp:pai):pai;
  547. const
  548. alignarray:array[0..5] of string[8]=(
  549. #$8D#$B4#$26#$00#$00#$00#$00,
  550. #$8D#$B6#$00#$00#$00#$00,
  551. #$8D#$74#$26#$00,
  552. #$8D#$76#$00,
  553. #$89#$F6,
  554. #$90
  555. );
  556. var
  557. l,j : longint;
  558. {$ifdef I386}
  559. co : comp;
  560. {$endif I386}
  561. begin
  562. { main loop }
  563. while assigned(hp) do
  564. begin
  565. {$ifdef GDB}
  566. { write stabs }
  567. if cs_debuginfo in aktmoduleswitches then
  568. begin
  569. if (objectoutput^.currsec<>sec_none) and
  570. not(hp^.typ in [
  571. ait_label,
  572. ait_regalloc,ait_tempalloc,
  573. ait_stabn,ait_stabs,ait_section,
  574. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  575. WriteFileLineInfo(hp^.fileinfo);
  576. end;
  577. {$endif GDB}
  578. case hp^.typ of
  579. ait_align :
  580. begin
  581. l:=pai_align(hp)^.fillsize;
  582. while (l>0) do
  583. begin
  584. for j:=0to 5 do
  585. if (l>=length(alignarray[j])) then
  586. break;
  587. objectoutput^.writebytes(alignarray[j][1],length(alignarray[j]));
  588. dec(l,length(alignarray[j]));
  589. end;
  590. end;
  591. ait_section :
  592. begin
  593. objectoutput^.defaultsection(pai_section(hp)^.sec);
  594. {$ifdef GDB}
  595. case pai_section(hp)^.sec of
  596. sec_code : n_line:=n_textline;
  597. sec_data : n_line:=n_dataline;
  598. sec_bss : n_line:=n_bssline;
  599. else
  600. n_line:=n_dataline;
  601. end;
  602. stabslastfileinfo.line:=-1;
  603. {$endif GDB}
  604. end;
  605. ait_symbol :
  606. objectoutput^.writesymbol(pai_symbol(hp)^.sym);
  607. ait_datablock :
  608. begin
  609. objectoutput^.writesymbol(pai_datablock(hp)^.sym);
  610. {$ifdef EXTERNALBSS}
  611. if not pai_datablock(hp)^.is_global then
  612. {$endif}
  613. begin
  614. l:=pai_datablock(hp)^.size;
  615. if l>2 then
  616. objectoutput^.writealign(4)
  617. else if l>1 then
  618. objectoutput^.writealign(2);
  619. objectoutput^.writealloc(pai_datablock(hp)^.size);
  620. end;
  621. end;
  622. ait_const_32bit :
  623. objectoutput^.writebytes(pai_const(hp)^.value,4);
  624. ait_const_16bit :
  625. objectoutput^.writebytes(pai_const(hp)^.value,2);
  626. ait_const_8bit :
  627. objectoutput^.writebytes(pai_const(hp)^.value,1);
  628. ait_real_80bit :
  629. objectoutput^.writebytes(pai_real_80bit(hp)^.value,10);
  630. ait_real_64bit :
  631. objectoutput^.writebytes(pai_real_64bit(hp)^.value,8);
  632. ait_real_32bit :
  633. objectoutput^.writebytes(pai_real_32bit(hp)^.value,4);
  634. ait_comp_64bit :
  635. begin
  636. {$ifdef FPC}
  637. co:=comp(pai_comp_64bit(hp)^.value);
  638. {$else}
  639. co:=pai_comp_64bit(hp)^.value;
  640. {$endif}
  641. objectoutput^.writebytes(co,8);
  642. end;
  643. ait_string :
  644. objectoutput^.writebytes(pai_string(hp)^.str^,pai_string(hp)^.len);
  645. ait_const_rva :
  646. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  647. pai_const_symbol(hp)^.sym,relative_rva);
  648. ait_const_symbol :
  649. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  650. pai_const_symbol(hp)^.sym,relative_false);
  651. ait_label :
  652. objectoutput^.writesymbol(pai_label(hp)^.l);
  653. ait_instruction :
  654. pai386(hp)^.Pass2;
  655. {$ifdef GDB}
  656. ait_stabn :
  657. convertstabs(pai_stabn(hp)^.str);
  658. ait_stabs :
  659. convertstabs(pai_stabs(hp)^.str);
  660. ait_stab_function_name :
  661. if assigned(pai_stab_function_name(hp)^.str) then
  662. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  663. else
  664. funcname:=nil;
  665. ait_force_line :
  666. stabslastfileinfo.line:=0;
  667. {$endif}
  668. ait_cut :
  669. if SmartAsm then
  670. break;
  671. end;
  672. hp:=pai(hp^.next);
  673. end;
  674. TreePass2:=hp;
  675. end;
  676. procedure ti386binasmlist.writetree;
  677. var
  678. hp : pai;
  679. begin
  680. objectalloc^.setsection(sec_code);
  681. objectoutput^.defaultsection(sec_code);
  682. { Pass 1 }
  683. currpass:=1;
  684. {$ifdef GDB}
  685. StartFileLineInfo;
  686. {$endif GDB}
  687. { start with list 1 }
  688. currlistidx:=1;
  689. currlist:=list[currlistidx];
  690. hp:=pai(currlist^.first);
  691. while assigned(hp) do
  692. begin
  693. hp:=TreePass1(hp);
  694. { maybe end of list }
  695. if not assigned(hp) then
  696. begin
  697. if currlistidx<lists then
  698. begin
  699. inc(currlistidx);
  700. currlist:=list[currlistidx];
  701. hp:=pai(currlist^.first);
  702. end
  703. else
  704. break;
  705. end;
  706. end;
  707. { set section sizes }
  708. objectoutput^.setsectionsizes(objectalloc^.secsize);
  709. { Pass 2 }
  710. currpass:=2;
  711. {$ifdef GDB}
  712. StartFileLineInfo;
  713. {$endif GDB}
  714. { start with list 1 }
  715. currlistidx:=1;
  716. currlist:=list[currlistidx];
  717. hp:=pai(currlist^.first);
  718. while assigned(hp) do
  719. begin
  720. hp:=TreePass2(hp);
  721. { maybe end of list }
  722. if not assigned(hp) then
  723. begin
  724. if currlistidx<lists then
  725. begin
  726. inc(currlistidx);
  727. currlist:=list[currlistidx];
  728. hp:=pai(currlist^.first);
  729. end
  730. else
  731. break;
  732. end;
  733. end;
  734. end;
  735. procedure ti386binasmlist.writetreesmart;
  736. var
  737. hp,hp1 : pai;
  738. begin
  739. objectalloc^.setsection(sec_code);
  740. objectoutput^.defaultsection(sec_code);
  741. { start with list 1 }
  742. currlistidx:=1;
  743. currlist:=list[currlistidx];
  744. hp:=pai(currlist^.first);
  745. while assigned(hp) do
  746. begin
  747. { Pass 1 }
  748. currpass:=1;
  749. {$ifdef GDB}
  750. StartFileLineInfo;
  751. {$endif GDB}
  752. hp1:=TreePass1(hp);
  753. { set section sizes }
  754. objectoutput^.setsectionsizes(objectalloc^.secsize);
  755. { Pass 2 }
  756. currpass:=2;
  757. {$ifdef GDB}
  758. StartFileLineInfo;
  759. {$endif GDB}
  760. hp:=TreePass2(hp);
  761. { maybe end of list }
  762. if not assigned(hp) then
  763. begin
  764. if currlistidx<lists then
  765. begin
  766. inc(currlistidx);
  767. currlist:=list[currlistidx];
  768. hp:=pai(currlist^.first);
  769. end
  770. else
  771. break;
  772. end;
  773. { write the current objectfile }
  774. objectoutput^.donewriting;
  775. { start the writing again }
  776. objectoutput^.initwriting;
  777. { we will start a new objectfile so reset everything }
  778. ResetAsmsymbolList;
  779. objectalloc^.resetsections;
  780. { avoid empty files }
  781. while assigned(hp^.next) and
  782. (pai(hp^.next)^.typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  783. begin
  784. if pai(hp^.next)^.typ=ait_section then
  785. begin
  786. objectalloc^.setsection(pai_section(hp^.next)^.sec);
  787. objectoutput^.defaultsection(pai_section(hp^.next)^.sec);
  788. end;
  789. hp:=pai(hp^.next);
  790. end;
  791. hp:=pai(hp^.next);
  792. end;
  793. end;
  794. procedure ti386binasmlist.writebin;
  795. procedure addlist(p:paasmoutput);
  796. begin
  797. inc(lists);
  798. list[lists]:=p;
  799. end;
  800. begin
  801. {$ifdef MULTIPASS}
  802. { Process the codesegment twice so the short jmp instructions can
  803. be optimized }
  804. currpass:=0;
  805. TreePass0(pai(codesegment^.first));
  806. {$endif}
  807. objectalloc^.resetsections;
  808. objectalloc^.setsection(sec_code);
  809. objectoutput^.initwriting;
  810. objectoutput^.defaultsection(sec_code);
  811. if cs_debuginfo in aktmoduleswitches then
  812. addlist(debuglist);
  813. addlist(codesegment);
  814. addlist(datasegment);
  815. addlist(consts);
  816. addlist(rttilist);
  817. if assigned(resourcestringlist) then
  818. addlist(resourcestringlist);
  819. addlist(bsssegment);
  820. if assigned(importssection) then
  821. addlist(importssection);
  822. if assigned(exportssection) then
  823. addlist(exportssection);
  824. if assigned(resourcesection) then
  825. addlist(resourcesection);
  826. if SmartAsm then
  827. writetreesmart
  828. else
  829. writetree;
  830. objectoutput^.donewriting;
  831. end;
  832. constructor ti386binasmlist.init(t:togtype;smart:boolean);
  833. begin
  834. case t of
  835. og_none :
  836. Message(asmw_f_no_binary_writer_selected);
  837. og_dbg :
  838. objectoutput:=new(pdbgoutput,init(smart));
  839. og_coff :
  840. objectoutput:=new(pdjgppcoffoutput,init(smart));
  841. og_pecoff :
  842. objectoutput:=new(pwin32coffoutput,init(smart));
  843. end;
  844. objectalloc:=new(pobjectalloc,init);
  845. SmartAsm:=smart;
  846. currpass:=0;
  847. end;
  848. destructor ti386binasmlist.done;
  849. begin
  850. dispose(objectoutput,done);
  851. dispose(objectalloc,done);
  852. end;
  853. end.
  854. {
  855. $Log$
  856. Revision 1.19 1999-07-22 09:37:30 florian
  857. + resourcestring implemented
  858. + start of longstring support
  859. Revision 1.18 1999/07/03 00:26:02 peter
  860. * ag386bin doesn't destroy the aasmoutput lists anymore
  861. Revision 1.17 1999/06/10 23:52:34 pierre
  862. * merged from fixes branch
  863. Revision 1.16.2.1 1999/06/10 23:33:35 pierre
  864. * pchar memory loss and .bss size problem solved
  865. Revision 1.16 1999/06/03 16:39:10 pierre
  866. * EXTERNALBSS fixed for stabs and default again
  867. Revision 1.15 1999/06/02 22:43:59 pierre
  868. * previous wrong log corrected
  869. Revision 1.14 1999/06/02 22:25:25 pierre
  870. * changed $ifdef FPC @ into $ifndef TP
  871. Revision 1.13 1999/06/01 10:24:09 pierre
  872. * ts010021.pp problem solved for ag386bin !
  873. Revision 1.12 1999/05/27 19:43:59 peter
  874. * removed oldasm
  875. * plabel -> pasmlabel
  876. * -a switches to source writing automaticly
  877. * assembler readers OOPed
  878. * asmsymbol automaticly external
  879. * jumptables and other label fixes for asm readers
  880. Revision 1.11 1999/05/21 13:54:41 peter
  881. * NEWLAB for label as symbol
  882. Revision 1.10 1999/05/19 11:54:17 pierre
  883. + experimental code for externalbss and stabs problem
  884. Revision 1.9 1999/05/12 00:19:37 peter
  885. * removed R_DEFAULT_SEG
  886. * uniform float names
  887. Revision 1.8 1999/05/09 11:38:04 peter
  888. * don't write .o and link if errors occure during assembling
  889. Revision 1.6 1999/05/07 00:36:58 pierre
  890. * added alignment code for .bss
  891. * stabs correct but externalbss disabled
  892. would need a special treatment in writestabs
  893. Revision 1.5 1999/05/06 09:05:07 peter
  894. * generic write_float and str_float
  895. * fixed constant float conversions
  896. Revision 1.4 1999/05/05 22:21:47 peter
  897. * updated messages
  898. Revision 1.3 1999/05/05 17:34:29 peter
  899. * output is more like as 2.9.1
  900. * stabs really working for go32v2
  901. Revision 1.2 1999/05/04 21:44:30 florian
  902. * changes to compile it with Delphi 4.0
  903. Revision 1.1 1999/05/01 13:23:57 peter
  904. * merged nasm compiler
  905. * old asm moved to oldasm/
  906. Revision 1.14 1999/04/16 11:49:48 peter
  907. + tempalloc
  908. + -at to show temp alloc info in .s file
  909. Revision 1.13 1999/03/12 00:20:03 pierre
  910. + win32 output working !
  911. Revision 1.12 1999/03/11 17:52:34 peter
  912. * fixed wrong ot_signed generation in insns tab
  913. Revision 1.11 1999/03/10 13:41:07 pierre
  914. + partial implementation for win32 !
  915. winhello works but pp still does not !
  916. Revision 1.10 1999/03/08 14:51:05 peter
  917. + smartlinking for ag386bin
  918. Revision 1.9 1999/03/06 17:24:18 peter
  919. * rewritten intel parser a lot, especially reference reading
  920. * size checking added for asm parsers
  921. Revision 1.8 1999/03/05 13:09:50 peter
  922. * first things for tai_cut support for ag386bin
  923. Revision 1.7 1999/03/03 11:41:53 pierre
  924. + stabs info corrected to give results near to GAS output
  925. * local labels (with .L are not stored in object anymore)
  926. so we get the same number of symbols as from GAS !
  927. Revision 1.6 1999/03/03 01:36:44 pierre
  928. + stabs output working (though not really tested)
  929. for a simple file the only difference to GAS output is due
  930. to the VMA of the different sections
  931. Revision 1.5 1999/03/02 02:56:18 peter
  932. + stabs support for binary writers
  933. * more fixes and missing updates from the previous commit :(
  934. Revision 1.4 1999/03/01 15:46:20 peter
  935. * ag386bin finally make cycles correct
  936. * prefixes are now also normal opcodes
  937. Revision 1.3 1999/02/25 21:03:01 peter
  938. * ag386bin updates
  939. + coff writer
  940. Revision 1.2 1999/02/22 02:16:00 peter
  941. * updates for ag386bin
  942. Revision 1.1 1999/02/16 17:59:37 peter
  943. + initial files
  944. }