ag386bin.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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,verbose,
  60. globtype,globals,
  61. i386asm,systems,
  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_64bit :
  337. objectalloc^.sectionalloc(8);
  338. ait_real_32bit :
  339. objectalloc^.sectionalloc(4);
  340. ait_real_80bit :
  341. objectalloc^.sectionalloc(10);
  342. ait_comp :
  343. {$ifdef I386}
  344. objectalloc^.sectionalloc(8);
  345. {$else not I386}
  346. Message(asmw_f_comp_not_supported);
  347. {$endif I386}
  348. ait_const_rva,
  349. ait_const_symbol :
  350. objectalloc^.sectionalloc(4);
  351. ait_section:
  352. begin
  353. objectalloc^.setsection(pai_section(hp)^.sec);
  354. lastsec:=pai_section(hp)^.sec;
  355. end;
  356. ait_symbol :
  357. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  358. ait_label :
  359. begin
  360. pai_label(hp)^.setaddress(objectalloc^.sectionsize);
  361. if pai_label(hp)^.l^.is_symbol then
  362. begin
  363. pai_label(hp)^.sym:=newasmsymbol(lab2str(pai_label(hp)^.l));
  364. if (pai_label(hp)^.l^.is_data) and (cs_smartlink in aktmoduleswitches) then
  365. pai_label(hp)^.sym^.typ:=AS_GLOBAL
  366. else
  367. pai_label(hp)^.sym^.typ:=AS_LOCAL;
  368. pai_label(hp)^.sym^.setaddress(objectalloc^.currsec,pai_label(hp)^.l^.address,0);
  369. end;
  370. end;
  371. ait_string :
  372. objectalloc^.sectionalloc(pai_string(hp)^.len);
  373. ait_labeled_instruction,
  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 [ait_external,ait_regalloc, ait_tempalloc,
  398. ait_stabn,ait_stabs,ait_section,
  399. ait_label,ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  400. WriteFileLineInfo(hp^.fileinfo);
  401. end;
  402. {$endif GDB}
  403. case hp^.typ of
  404. ait_align :
  405. begin
  406. if (objectalloc^.sectionsize mod pai_align(hp)^.aligntype)<>0 then
  407. begin
  408. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype-
  409. (objectalloc^.sectionsize mod pai_align(hp)^.aligntype);
  410. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  411. end
  412. else
  413. pai_align(hp)^.fillsize:=0;
  414. end;
  415. ait_datablock :
  416. begin
  417. if objectalloc^.currsec<>sec_bss then
  418. Message(asmw_e_alloc_data_only_in_bss);
  419. {$ifdef EXTERNALBSS}
  420. if pai_datablock(hp)^.is_global then
  421. begin
  422. pai_datablock(hp)^.sym^.typ:=AS_EXTERNAL;
  423. pai_datablock(hp)^.sym^.setaddress(sec_none,pai_datablock(hp)^.size,pai_datablock(hp)^.size);
  424. end
  425. else
  426. begin
  427. l:=pai_datablock(hp)^.size;
  428. if l>2 then
  429. objectalloc^.sectionalign(4)
  430. else if l>1 then
  431. objectalloc^.sectionalign(2);
  432. pai_datablock(hp)^.sym^.typ:=AS_LOCAL;
  433. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  434. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  435. end;
  436. {$else}
  437. if pai_datablock(hp)^.is_global then
  438. pai_datablock(hp)^.sym^.typ:=AS_GLOBAL
  439. else
  440. pai_datablock(hp)^.sym^.typ:=AS_LOCAL;
  441. l:=pai_datablock(hp)^.size;
  442. if l>2 then
  443. objectalloc^.sectionalign(4)
  444. else if l>1 then
  445. objectalloc^.sectionalign(2);
  446. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  447. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  448. {$endif}
  449. end;
  450. ait_const_32bit :
  451. objectalloc^.sectionalloc(4);
  452. ait_const_16bit :
  453. objectalloc^.sectionalloc(2);
  454. ait_const_8bit :
  455. objectalloc^.sectionalloc(1);
  456. ait_real_64bit :
  457. objectalloc^.sectionalloc(8);
  458. ait_real_32bit :
  459. objectalloc^.sectionalloc(4);
  460. ait_real_80bit :
  461. objectalloc^.sectionalloc(10);
  462. ait_comp :
  463. {$ifdef I386}
  464. objectalloc^.sectionalloc(8);
  465. {$else not I386}
  466. Message(asmw_f_comp_not_supported);
  467. {$endif I386}
  468. ait_const_rva,
  469. ait_const_symbol :
  470. objectalloc^.sectionalloc(4);
  471. ait_external :
  472. pai_external(hp)^.sym^.typ:=AS_EXTERNAL;
  473. ait_section:
  474. begin
  475. objectalloc^.setsection(pai_section(hp)^.sec);
  476. {$ifdef GDB}
  477. case pai_section(hp)^.sec of
  478. sec_code : n_line:=n_textline;
  479. sec_data : n_line:=n_dataline;
  480. sec_bss : n_line:=n_bssline;
  481. else
  482. n_line:=n_dataline;
  483. end;
  484. stabslastfileinfo.line:=-1;
  485. {$endif GDB}
  486. end;
  487. {$ifdef GDB}
  488. ait_stabn :
  489. convertstabs(pai_stabn(hp)^.str);
  490. ait_stabs :
  491. convertstabs(pai_stabs(hp)^.str);
  492. ait_stab_function_name :
  493. if assigned(pai_stab_function_name(hp)^.str) then
  494. funcname:=getasmsymbol(pai_stab_function_name(hp)^.str)
  495. else
  496. funcname:=nil;
  497. ait_force_line :
  498. stabslastfileinfo.line:=0;
  499. {$endif}
  500. ait_symbol :
  501. begin
  502. if pai_symbol(hp)^.is_global then
  503. pai_symbol(hp)^.sym^.typ:=AS_GLOBAL
  504. else
  505. pai_symbol(hp)^.sym^.typ:=AS_LOCAL;
  506. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  507. end;
  508. ait_label :
  509. begin
  510. pai_label(hp)^.setaddress(objectalloc^.sectionsize);
  511. if pai_label(hp)^.l^.is_symbol then
  512. begin
  513. pai_label(hp)^.sym:=newasmsymbol(lab2str(pai_label(hp)^.l));
  514. if (pai_label(hp)^.l^.is_data) and (cs_smartlink in aktmoduleswitches) then
  515. pai_label(hp)^.sym^.typ:=AS_GLOBAL
  516. else
  517. pai_label(hp)^.sym^.typ:=AS_LOCAL;
  518. pai_label(hp)^.sym^.setaddress(objectalloc^.currsec,pai_label(hp)^.l^.address,0);
  519. end;
  520. end;
  521. ait_string :
  522. objectalloc^.sectionalloc(pai_string(hp)^.len);
  523. ait_labeled_instruction,
  524. ait_instruction :
  525. objectalloc^.sectionalloc(pai386(hp)^.Pass1(objectalloc^.sectionsize));
  526. ait_direct :
  527. Message(asmw_f_direct_not_supported);
  528. ait_cut :
  529. break;
  530. end;
  531. hp:=pai(hp^.next);
  532. end;
  533. TreePass1:=hp;
  534. end;
  535. function ti386binasmlist.TreePass2(hp:pai):pai;
  536. const
  537. alignarray:array[0..5] of string[8]=(
  538. #$8D#$B4#$26#$00#$00#$00#$00,
  539. #$8D#$B6#$00#$00#$00#$00,
  540. #$8D#$74#$26#$00,
  541. #$8D#$76#$00,
  542. #$89#$F6,
  543. #$90
  544. );
  545. var
  546. l,j : longint;
  547. {$ifdef I386}
  548. co : comp;
  549. {$endif I386}
  550. begin
  551. { main loop }
  552. while assigned(hp) do
  553. begin
  554. {$ifdef GDB}
  555. { write stabs }
  556. if cs_debuginfo in aktmoduleswitches then
  557. begin
  558. if (objectoutput^.currsec<>sec_none) and
  559. not(hp^.typ in [ait_external,ait_regalloc, ait_tempalloc,
  560. ait_stabn,ait_stabs,ait_section,
  561. ait_label,ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  562. WriteFileLineInfo(hp^.fileinfo);
  563. end;
  564. {$endif GDB}
  565. case hp^.typ of
  566. ait_align :
  567. begin
  568. l:=pai_align(hp)^.fillsize;
  569. while (l>0) do
  570. begin
  571. for j:=0to 5 do
  572. if (l>=length(alignarray[j])) then
  573. break;
  574. objectoutput^.writebytes(alignarray[j][1],length(alignarray[j]));
  575. dec(l,length(alignarray[j]));
  576. end;
  577. end;
  578. ait_section :
  579. begin
  580. objectoutput^.defaultsection(pai_section(hp)^.sec);
  581. {$ifdef GDB}
  582. case pai_section(hp)^.sec of
  583. sec_code : n_line:=n_textline;
  584. sec_data : n_line:=n_dataline;
  585. sec_bss : n_line:=n_bssline;
  586. else
  587. n_line:=n_dataline;
  588. end;
  589. stabslastfileinfo.line:=-1;
  590. {$endif GDB}
  591. end;
  592. ait_external :
  593. objectoutput^.writesymbol(pai_external(hp)^.sym);
  594. ait_symbol :
  595. objectoutput^.writesymbol(pai_symbol(hp)^.sym);
  596. ait_datablock :
  597. begin
  598. l:=pai_datablock(hp)^.size;
  599. if l>2 then
  600. objectoutput^.writealign(4)
  601. else if l>1 then
  602. objectoutput^.writealign(2);
  603. objectoutput^.writesymbol(pai_datablock(hp)^.sym);
  604. {$ifdef EXTERNALBSS}
  605. if not pai_datablock(hp)^.is_global then
  606. {$endif}
  607. objectoutput^.writealloc(pai_datablock(hp)^.size);
  608. end;
  609. ait_const_32bit :
  610. objectoutput^.writebytes(pai_const(hp)^.value,4);
  611. ait_const_16bit :
  612. objectoutput^.writebytes(pai_const(hp)^.value,2);
  613. ait_const_8bit :
  614. objectoutput^.writebytes(pai_const(hp)^.value,1);
  615. ait_real_64bit :
  616. objectoutput^.writebytes(pai_double(hp)^.value,8);
  617. ait_real_32bit :
  618. objectoutput^.writebytes(pai_single(hp)^.value,4);
  619. ait_real_80bit :
  620. objectoutput^.writebytes(pai_extended(hp)^.value,10);
  621. {$ifdef I386}
  622. ait_comp :
  623. begin
  624. {$ifdef FPC}
  625. co:=comp(pai_comp(hp)^.value);
  626. {$else}
  627. co:=pai_comp(hp)^.value;
  628. {$endif}
  629. objectoutput^.writebytes(co,8);
  630. end;
  631. {$endif I386}
  632. ait_string :
  633. objectoutput^.writebytes(pai_string(hp)^.str^,pai_string(hp)^.len);
  634. ait_const_rva :
  635. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  636. pai_const_symbol(hp)^.sym,relative_rva);
  637. ait_const_symbol :
  638. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  639. pai_const_symbol(hp)^.sym,relative_false);
  640. ait_label :
  641. begin
  642. if assigned(pai_label(hp)^.sym) then
  643. objectoutput^.writesymbol(pai_label(hp)^.sym);
  644. end;
  645. ait_labeled_instruction,
  646. ait_instruction :
  647. pai386(hp)^.Pass2;
  648. {$ifdef GDB}
  649. ait_stabn :
  650. convertstabs(pai_stabn(hp)^.str);
  651. ait_stabs :
  652. convertstabs(pai_stabs(hp)^.str);
  653. ait_stab_function_name :
  654. if assigned(pai_stab_function_name(hp)^.str) then
  655. funcname:=getasmsymbol(pai_stab_function_name(hp)^.str)
  656. else
  657. funcname:=nil;
  658. ait_force_line :
  659. stabslastfileinfo.line:=0;
  660. {$endif}
  661. ait_cut :
  662. break;
  663. end;
  664. hp:=pai(hp^.next);
  665. end;
  666. TreePass2:=hp;
  667. end;
  668. procedure ti386binasmlist.writetree(p:paasmoutput);
  669. var
  670. hp,hp1 : pai;
  671. begin
  672. if not assigned(p) then
  673. exit;
  674. objectalloc^.setsection(sec_code);
  675. objectoutput^.defaultsection(sec_code);
  676. hp:=pai(p^.first);
  677. while assigned(hp) do
  678. begin
  679. { Pass 1 }
  680. currpass:=1;
  681. {$ifdef GDB}
  682. StartFileLineInfo;
  683. {$endif GDB}
  684. hp1:=TreePass1(hp);
  685. { set section sizes }
  686. objectoutput^.setsectionsizes(objectalloc^.secsize);
  687. { Pass 2 }
  688. currpass:=2;
  689. {$ifdef GDB}
  690. StartFileLineInfo;
  691. {$endif GDB}
  692. hp1:=TreePass2(hp);
  693. { if assigned then we have a ait_cut }
  694. hp:=hp1;
  695. if assigned(hp) then
  696. begin
  697. if hp^.typ<>ait_cut then
  698. internalerror(3334443);
  699. { write the current objectfile }
  700. objectoutput^.donewriting;
  701. { start the writing again }
  702. objectoutput^.initwriting;
  703. { we will start a new objectfile so reset everything }
  704. ResetAsmsymbolList;
  705. objectalloc^.resetsections;
  706. { avoid empty files }
  707. while assigned(hp^.next) and
  708. (pai(hp^.next)^.typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  709. begin
  710. if pai(hp^.next)^.typ=ait_section then
  711. begin
  712. objectalloc^.setsection(pai_section(hp^.next)^.sec);
  713. objectoutput^.defaultsection(pai_section(hp^.next)^.sec);
  714. end;
  715. hp:=pai(hp^.next);
  716. end;
  717. hp:=pai(hp^.next);
  718. end;
  719. end;
  720. end;
  721. procedure ti386binasmlist.writebin;
  722. var
  723. mylist : paasmoutput;
  724. procedure addlist(p:paasmoutput);
  725. begin
  726. mylist^.concat(new(pai_section,init(sec_code)));
  727. mylist^.concatlist(p);
  728. end;
  729. begin
  730. {$ifdef MULTIPASS}
  731. { Process the codesegment twice so the short jmp instructions can
  732. be optimized }
  733. currpass:=0;
  734. TreePass0(pai(codesegment^.first));
  735. {$endif}
  736. objectalloc^.resetsections;
  737. objectalloc^.setsection(sec_code);
  738. objectoutput^.initwriting;
  739. objectoutput^.defaultsection(sec_code);
  740. new(mylist,init);
  741. if not(cs_compilesystem in aktmoduleswitches) then
  742. addlist(externals);
  743. if cs_debuginfo in aktmoduleswitches then
  744. addlist(debuglist);
  745. addlist(codesegment);
  746. addlist(datasegment);
  747. addlist(consts);
  748. addlist(rttilist);
  749. addlist(bsssegment);
  750. if assigned(importssection) then
  751. addlist(importssection);
  752. if assigned(exportssection) then
  753. addlist(exportssection);
  754. if assigned(resourcesection) then
  755. addlist(resourcesection);
  756. WriteTree(mylist);
  757. dispose(mylist,done);
  758. objectoutput^.donewriting;
  759. end;
  760. constructor ti386binasmlist.init(t:togtype);
  761. begin
  762. case t of
  763. og_none :
  764. Message(asmw_f_no_binary_writer_selected);
  765. og_dbg :
  766. objectoutput:=new(pdbgoutput,init);
  767. og_coff :
  768. objectoutput:=new(pdjgppcoffoutput,init);
  769. og_pecoff :
  770. objectoutput:=new(pwin32coffoutput,init);
  771. end;
  772. objectalloc:=new(pobjectalloc,init);
  773. currpass:=0;
  774. end;
  775. destructor ti386binasmlist.done;
  776. begin
  777. dispose(objectoutput,done);
  778. dispose(objectalloc,done);
  779. end;
  780. end.
  781. {
  782. $Log$
  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. }