ag386bin.pas 29 KB

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