ag386bin.pas 32 KB

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