ag386bin.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. cpubase,cobjects,aasm,files,assemble;
  27. type
  28. togtype=(og_none,og_dbg,og_coff,og_pecoff);
  29. pi386binasmlist=^ti386binasmlist;
  30. ti386binasmlist=object
  31. SmartAsm : boolean;
  32. constructor init(t:togtype;smart:boolean);
  33. destructor done;
  34. procedure WriteBin;
  35. private
  36. { the aasmoutput lists that need to be processed }
  37. lists : byte;
  38. list : array[1..maxoutputlists] of paasmoutput;
  39. { current processing }
  40. currlistidx : byte;
  41. currlist : paasmoutput;
  42. currpass : byte;
  43. {$ifdef GDB}
  44. n_line : byte; { different types of source lines }
  45. linecount,
  46. includecount : longint;
  47. funcname : pasmsymbol;
  48. stabslastfileinfo : tfileposinfo;
  49. procedure convertstabs(p:pchar);
  50. {$ifdef unused}
  51. procedure emitsymbolstabs(s : string;nidx,nother,line : longint;firstasm,secondasm : pasmsymbol);
  52. {$endif}
  53. procedure emitlineinfostabs(nidx,line : longint);
  54. procedure emitstabs(s:string);
  55. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  56. procedure StartFileLineInfo;
  57. {$endif}
  58. function MaybeNextList(var hp:pai):boolean;
  59. function TreePass0(hp:pai):pai;
  60. function TreePass1(hp:pai):pai;
  61. function TreePass2(hp:pai):pai;
  62. procedure writetree;
  63. procedure writetreesmart;
  64. end;
  65. implementation
  66. uses
  67. strings,
  68. globtype,globals,systems,verbose,
  69. cpuasm,
  70. {$ifdef GDB}
  71. gdb,
  72. {$endif}
  73. og386,og386dbg,og386cff;
  74. {$ifdef GDB}
  75. procedure ti386binasmlist.convertstabs(p:pchar);
  76. var
  77. ofs,
  78. nidx,nother,ii,i,line,j : longint;
  79. code : integer;
  80. hp : pchar;
  81. reloc : boolean;
  82. sec : tsection;
  83. ps : pasmsymbol;
  84. s : string;
  85. begin
  86. ofs:=0;
  87. reloc:=true;
  88. ps:=nil;
  89. sec:=sec_none;
  90. if p[0]='"' then
  91. begin
  92. i:=1;
  93. { we can have \" inside the string !! PM }
  94. while not ((p[i]='"') and (p[i-1]<>'\')) do
  95. inc(i);
  96. p[i]:=#0;
  97. ii:=i;
  98. hp:=@p[1];
  99. s:=StrPas(@P[i+2]);
  100. end
  101. else
  102. begin
  103. hp:=nil;
  104. s:=StrPas(P);
  105. i:=-2; {needed below (PM) }
  106. end;
  107. { When in pass 1 then only alloc and leave }
  108. if currpass=1 then
  109. begin
  110. objectalloc^.staballoc(hp);
  111. if assigned(hp) then
  112. p[i]:='"';
  113. exit;
  114. end;
  115. { Parse the rest of the stabs }
  116. if s='' then
  117. internalerror(33000);
  118. j:=pos(',',s);
  119. if j=0 then
  120. internalerror(33001);
  121. Val(Copy(s,1,j-1),nidx,code);
  122. if code<>0 then
  123. internalerror(33002);
  124. i:=i+2+j;
  125. Delete(s,1,j);
  126. j:=pos(',',s);
  127. if (j=0) then
  128. internalerror(33003);
  129. Val(Copy(s,1,j-1),nother,code);
  130. if code<>0 then
  131. internalerror(33004);
  132. i:=i+j;
  133. Delete(s,1,j);
  134. j:=pos(',',s);
  135. if j=0 then
  136. begin
  137. j:=256;
  138. ofs:=-1;
  139. end;
  140. Val(Copy(s,1,j-1),line,code);
  141. if code<>0 then
  142. internalerror(33005);
  143. if ofs=0 then
  144. begin
  145. Delete(s,1,j);
  146. i:=i+j;
  147. Val(s,ofs,code);
  148. if code=0 then
  149. reloc:=false
  150. else
  151. begin
  152. ofs:=0;
  153. s:=strpas(@p[i]);
  154. { handle asmsymbol or
  155. asmsymbol - asmsymbol }
  156. j:=pos(' ',s);
  157. if j=0 then
  158. j:=pos('-',s);
  159. { single asmsymbol }
  160. if j=0 then
  161. j:=256;
  162. ps:=getasmsymbol(copy(s,1,j-1));
  163. if not assigned(ps) then
  164. internalerror(33006)
  165. else
  166. begin
  167. sec:=ps^.section;
  168. ofs:=ps^.address;
  169. reloc:=true;
  170. end;
  171. if j<256 then
  172. begin
  173. i:=i+j;
  174. s:=strpas(@p[i]);
  175. if (s<>'') and (s[1]=' ') then
  176. begin
  177. j:=0;
  178. while (s[j+1]=' ') do
  179. inc(j);
  180. i:=i+j;
  181. s:=strpas(@p[i]);
  182. end;
  183. ps:=getasmsymbol(s);
  184. if not assigned(ps) then
  185. internalerror(33007)
  186. else
  187. begin
  188. if ps^.section<>sec then
  189. internalerror(33008);
  190. ofs:=ofs-ps^.address;
  191. reloc:=false;
  192. end;
  193. end;
  194. end;
  195. end;
  196. { external bss need speical handling (PM) }
  197. if assigned(ps) and (ps^.section=sec_none) then
  198. objectoutput^.WriteSymStabs(sec,ofs,hp,ps,nidx,nother,line,reloc)
  199. else
  200. objectoutput^.WriteStabs(sec,ofs,hp,nidx,nother,line,reloc);
  201. if assigned(hp) then
  202. p[ii]:='"';
  203. end;
  204. {$ifdef unused}
  205. procedure ti386binasmlist.emitsymbolstabs(s : string;nidx,nother,line : longint;
  206. firstasm,secondasm : pasmsymbol);
  207. var
  208. hp : pchar;
  209. begin
  210. if s='' then
  211. hp:=nil
  212. else
  213. begin
  214. s:=s+#0;
  215. hp:=@s[1];
  216. end;
  217. if not assigned(secondasm) then
  218. begin
  219. if not assigned(firstasm) then
  220. internalerror(33009);
  221. objectoutput^.WriteStabs(firstasm^.section,firstasm^.address,hp,nidx,nother,line,true);
  222. end
  223. else
  224. begin
  225. if firstasm^.section<>secondasm^.section then
  226. internalerror(33010);
  227. objectoutput^.WriteStabs(firstasm^.section,firstasm^.address-secondasm^.address,
  228. hp,nidx,nother,line,false);
  229. end;
  230. end;
  231. {$endif}
  232. procedure ti386binasmlist.emitlineinfostabs(nidx,line : longint);
  233. var
  234. sec : tsection;
  235. begin
  236. if currpass=1 then
  237. begin
  238. objectalloc^.staballoc(nil);
  239. exit;
  240. end;
  241. if (nidx=n_textline) and assigned(funcname) and
  242. (target_os.use_function_relative_addresses) then
  243. objectoutput^.WriteStabs(sec_code,pgenericcoffoutput(objectoutput)^.sects[sec_code]^.len-funcname^.address,
  244. nil,nidx,0,line,false)
  245. else
  246. begin
  247. if nidx=n_textline then
  248. sec:=sec_code
  249. else if nidx=n_dataline then
  250. sec:=sec_data
  251. else
  252. sec:=sec_bss;
  253. objectoutput^.WriteStabs(sec,pgenericcoffoutput(objectoutput)^.sects[sec]^.len,
  254. nil,nidx,0,line,true);
  255. end;
  256. end;
  257. procedure ti386binasmlist.emitstabs(s:string);
  258. begin
  259. s:=s+#0;
  260. ConvertStabs(@s[1]);
  261. end;
  262. procedure ti386binasmlist.WriteFileLineInfo(var fileinfo : tfileposinfo);
  263. var
  264. curr_n : byte;
  265. hp : pasmsymbol;
  266. infile : pinputfile;
  267. begin
  268. if not (cs_debuginfo in aktmoduleswitches) then
  269. exit;
  270. { file changed ? (must be before line info) }
  271. if (fileinfo.fileindex<>0) and
  272. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  273. begin
  274. infile:=current_module^.sourcefiles^.get_file(fileinfo.fileindex);
  275. if includecount=0 then
  276. curr_n:=n_sourcefile
  277. else
  278. curr_n:=n_includefile;
  279. { get symbol for this includefile }
  280. hp:=newasmsymbol('Ltext'+ToStr(IncludeCount));
  281. if currpass=1 then
  282. begin
  283. hp^.typ:=AS_LOCAL;
  284. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  285. end
  286. else
  287. objectoutput^.writesymbol(hp);
  288. { emit stabs }
  289. if (infile^.path^<>'') then
  290. EmitStabs('"'+lower(BsToSlash(FixPath(infile^.path^,false)))+'",'+tostr(curr_n)+
  291. ',0,0,Ltext'+ToStr(IncludeCount));
  292. EmitStabs('"'+lower(FixFileName(infile^.name^))+'",'+tostr(curr_n)+
  293. ',0,0,Ltext'+ToStr(IncludeCount));
  294. inc(includecount);
  295. end;
  296. { line changed ? }
  297. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  298. emitlineinfostabs(n_line,fileinfo.line);
  299. stabslastfileinfo:=fileinfo;
  300. end;
  301. procedure ti386binasmlist.StartFileLineInfo;
  302. var
  303. fileinfo : tfileposinfo;
  304. begin
  305. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  306. n_line:=n_textline;
  307. funcname:=nil;
  308. linecount:=1;
  309. includecount:=0;
  310. fileinfo.fileindex:=1;
  311. fileinfo.line:=1;
  312. WriteFileLineInfo(fileinfo);
  313. end;
  314. {$endif GDB}
  315. function ti386binasmlist.MaybeNextList(var hp:pai):boolean;
  316. begin
  317. { maybe end of list }
  318. while not assigned(hp) do
  319. begin
  320. if currlistidx<lists then
  321. begin
  322. inc(currlistidx);
  323. currlist:=list[currlistidx];
  324. hp:=pai(currlist^.first);
  325. end
  326. else
  327. begin
  328. MaybeNextList:=false;
  329. exit;
  330. end;
  331. end;
  332. MaybeNextList:=true;
  333. end;
  334. function ti386binasmlist.TreePass0(hp:pai):pai;
  335. var
  336. lastsec : tsection;
  337. l : longint;
  338. begin
  339. while assigned(hp) do
  340. begin
  341. case hp^.typ of
  342. ait_align :
  343. begin
  344. if (objectalloc^.sectionsize mod pai_align(hp)^.aligntype)<>0 then
  345. begin
  346. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype-
  347. (objectalloc^.sectionsize mod pai_align(hp)^.aligntype);
  348. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  349. end
  350. else
  351. pai_align(hp)^.fillsize:=0;
  352. end;
  353. ait_datablock :
  354. begin
  355. {$ifdef EXTERNALBSS}
  356. if not SmartAsm then
  357. begin
  358. if not pai_datablock(hp)^.is_global then
  359. begin
  360. l:=pai_datablock(hp)^.size;
  361. if l>2 then
  362. objectalloc^.sectionalign(4)
  363. else if l>1 then
  364. objectalloc^.sectionalign(2);
  365. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  366. end;
  367. end
  368. else
  369. begin
  370. {$endif}
  371. l:=pai_datablock(hp)^.size;
  372. if l>2 then
  373. objectalloc^.sectionalign(4)
  374. else if l>1 then
  375. objectalloc^.sectionalign(2);
  376. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  377. end;
  378. end;
  379. ait_const_32bit :
  380. objectalloc^.sectionalloc(4);
  381. ait_const_16bit :
  382. objectalloc^.sectionalloc(2);
  383. ait_const_8bit :
  384. objectalloc^.sectionalloc(1);
  385. ait_real_80bit :
  386. objectalloc^.sectionalloc(10);
  387. ait_real_64bit :
  388. objectalloc^.sectionalloc(8);
  389. ait_real_32bit :
  390. objectalloc^.sectionalloc(4);
  391. ait_comp_64bit :
  392. objectalloc^.sectionalloc(8);
  393. ait_const_rva,
  394. ait_const_symbol :
  395. objectalloc^.sectionalloc(4);
  396. ait_section:
  397. begin
  398. objectalloc^.setsection(pai_section(hp)^.sec);
  399. lastsec:=pai_section(hp)^.sec;
  400. end;
  401. ait_symbol :
  402. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  403. ait_label :
  404. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  405. ait_string :
  406. objectalloc^.sectionalloc(pai_string(hp)^.len);
  407. ait_instruction :
  408. objectalloc^.sectionalloc(pai386(hp)^.Pass1(objectalloc^.sectionsize));
  409. ait_cut :
  410. if SmartAsm then
  411. begin
  412. objectalloc^.resetsections;
  413. objectalloc^.setsection(lastsec);
  414. end;
  415. end;
  416. hp:=pai(hp^.next);
  417. end;
  418. TreePass0:=hp;
  419. end;
  420. function ti386binasmlist.TreePass1(hp:pai):pai;
  421. var
  422. l : longint;
  423. begin
  424. while assigned(hp) do
  425. begin
  426. {$ifdef GDB}
  427. { write stabs }
  428. if (cs_debuginfo in aktmoduleswitches) then
  429. begin
  430. if (objectalloc^.currsec<>sec_none) and
  431. not(hp^.typ in [
  432. ait_label,
  433. ait_regalloc,ait_tempalloc,
  434. ait_stabn,ait_stabs,ait_section,
  435. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  436. WriteFileLineInfo(hp^.fileinfo);
  437. end;
  438. {$endif GDB}
  439. case hp^.typ of
  440. ait_align :
  441. begin
  442. if (objectalloc^.sectionsize mod pai_align(hp)^.aligntype)<>0 then
  443. begin
  444. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype-
  445. (objectalloc^.sectionsize mod pai_align(hp)^.aligntype);
  446. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  447. end
  448. else
  449. pai_align(hp)^.fillsize:=0;
  450. end;
  451. ait_datablock :
  452. begin
  453. if objectalloc^.currsec<>sec_bss then
  454. Message(asmw_e_alloc_data_only_in_bss);
  455. {$ifdef EXTERNALBSS}
  456. if not SmartAsm then
  457. begin
  458. if pai_datablock(hp)^.is_global then
  459. begin
  460. pai_datablock(hp)^.sym^.typ:=AS_EXTERNAL;
  461. pai_datablock(hp)^.sym^.setaddress(sec_none,pai_datablock(hp)^.size,pai_datablock(hp)^.size);
  462. end
  463. else
  464. begin
  465. l:=pai_datablock(hp)^.size;
  466. if l>2 then
  467. objectalloc^.sectionalign(4)
  468. else if l>1 then
  469. objectalloc^.sectionalign(2);
  470. pai_datablock(hp)^.sym^.typ:=AS_LOCAL;
  471. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,
  472. 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.22 1999-08-04 00:22:35 florian
  848. * renamed i386asm and i386base to cpuasm and cpubase
  849. Revision 1.21 1999/08/01 18:28:09 florian
  850. * modifications for the new code generator
  851. Revision 1.20 1999/07/31 12:33:11 peter
  852. * fixed smartlinking
  853. Revision 1.19 1999/07/22 09:37:30 florian
  854. + resourcestring implemented
  855. + start of longstring support
  856. Revision 1.18 1999/07/03 00:26:02 peter
  857. * ag386bin doesn't destroy the aasmoutput lists anymore
  858. Revision 1.17 1999/06/10 23:52:34 pierre
  859. * merged from fixes branch
  860. Revision 1.16.2.1 1999/06/10 23:33:35 pierre
  861. * pchar memory loss and .bss size problem solved
  862. Revision 1.16 1999/06/03 16:39:10 pierre
  863. * EXTERNALBSS fixed for stabs and default again
  864. Revision 1.15 1999/06/02 22:43:59 pierre
  865. * previous wrong log corrected
  866. Revision 1.14 1999/06/02 22:25:25 pierre
  867. * changed $ifdef FPC @ into $ifndef TP
  868. Revision 1.13 1999/06/01 10:24:09 pierre
  869. * ts010021.pp problem solved for ag386bin !
  870. Revision 1.12 1999/05/27 19:43:59 peter
  871. * removed oldasm
  872. * plabel -> pasmlabel
  873. * -a switches to source writing automaticly
  874. * assembler readers OOPed
  875. * asmsymbol automaticly external
  876. * jumptables and other label fixes for asm readers
  877. Revision 1.11 1999/05/21 13:54:41 peter
  878. * NEWLAB for label as symbol
  879. Revision 1.10 1999/05/19 11:54:17 pierre
  880. + experimental code for externalbss and stabs problem
  881. Revision 1.9 1999/05/12 00:19:37 peter
  882. * removed R_DEFAULT_SEG
  883. * uniform float names
  884. Revision 1.8 1999/05/09 11:38:04 peter
  885. * don't write .o and link if errors occure during assembling
  886. Revision 1.6 1999/05/07 00:36:58 pierre
  887. * added alignment code for .bss
  888. * stabs correct but externalbss disabled
  889. would need a special treatment in writestabs
  890. Revision 1.5 1999/05/06 09:05:07 peter
  891. * generic write_float and str_float
  892. * fixed constant float conversions
  893. Revision 1.4 1999/05/05 22:21:47 peter
  894. * updated messages
  895. Revision 1.3 1999/05/05 17:34:29 peter
  896. * output is more like as 2.9.1
  897. * stabs really working for go32v2
  898. Revision 1.2 1999/05/04 21:44:30 florian
  899. * changes to compile it with Delphi 4.0
  900. Revision 1.1 1999/05/01 13:23:57 peter
  901. * merged nasm compiler
  902. * old asm moved to oldasm/
  903. Revision 1.14 1999/04/16 11:49:48 peter
  904. + tempalloc
  905. + -at to show temp alloc info in .s file
  906. Revision 1.13 1999/03/12 00:20:03 pierre
  907. + win32 output working !
  908. Revision 1.12 1999/03/11 17:52:34 peter
  909. * fixed wrong ot_signed generation in insns tab
  910. Revision 1.11 1999/03/10 13:41:07 pierre
  911. + partial implementation for win32 !
  912. winhello works but pp still does not !
  913. Revision 1.10 1999/03/08 14:51:05 peter
  914. + smartlinking for ag386bin
  915. Revision 1.9 1999/03/06 17:24:18 peter
  916. * rewritten intel parser a lot, especially reference reading
  917. * size checking added for asm parsers
  918. Revision 1.8 1999/03/05 13:09:50 peter
  919. * first things for tai_cut support for ag386bin
  920. Revision 1.7 1999/03/03 11:41:53 pierre
  921. + stabs info corrected to give results near to GAS output
  922. * local labels (with .L are not stored in object anymore)
  923. so we get the same number of symbols as from GAS !
  924. Revision 1.6 1999/03/03 01:36:44 pierre
  925. + stabs output working (though not really tested)
  926. for a simple file the only difference to GAS output is due
  927. to the VMA of the different sections
  928. Revision 1.5 1999/03/02 02:56:18 peter
  929. + stabs support for binary writers
  930. * more fixes and missing updates from the previous commit :(
  931. Revision 1.4 1999/03/01 15:46:20 peter
  932. * ag386bin finally make cycles correct
  933. * prefixes are now also normal opcodes
  934. Revision 1.3 1999/02/25 21:03:01 peter
  935. * ag386bin updates
  936. + coff writer
  937. Revision 1.2 1999/02/22 02:16:00 peter
  938. * updates for ag386bin
  939. Revision 1.1 1999/02/16 17:59:37 peter
  940. + initial files
  941. }