ag386bin.pas 29 KB

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