ag386bin.pas 29 KB

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