assemble.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit handles the assemblerfile write and assembler calls of FPC
  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. unit assemble;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. {$ifdef Delphi}
  23. sysutils,
  24. dmisc,
  25. {$else Delphi}
  26. strings,
  27. dos,
  28. {$endif Delphi}
  29. cobjects,globtype,globals,aasm;
  30. const
  31. AsmOutSize=32768;
  32. type
  33. TAssembler=class
  34. private
  35. procedure CreateSmartLinkPath(const s:string);
  36. public
  37. {filenames}
  38. path : pathstr;
  39. name : namestr;
  40. asmfile, { current .s and .o file }
  41. objfile : string;
  42. SmartAsm : boolean;
  43. SmartFilesCount,
  44. SmartHeaderCount : longint;
  45. Constructor Create(smart:boolean);
  46. Destructor Destroy;override;
  47. procedure WriteTree(p:TAAsmoutput);virtual;
  48. procedure WriteAsmList;virtual;
  49. procedure NextSmartName(place:tcutplace);
  50. end;
  51. TExternalAssembler=class(TAssembler)
  52. protected
  53. {outfile}
  54. AsmSize,
  55. AsmStartSize,
  56. outcnt : longint;
  57. outbuf : array[0..AsmOutSize-1] of char;
  58. outfile : file;
  59. public
  60. Function FindAssembler:string;
  61. Function CallAssembler(const command,para:string):Boolean;
  62. Function DoAssemble:boolean;
  63. Procedure RemoveAsm;
  64. Procedure AsmFlush;
  65. Procedure AsmClear;
  66. Procedure AsmWrite(const s:string);
  67. Procedure AsmWritePChar(p:pchar);
  68. Procedure AsmWriteLn(const s:string);
  69. Procedure AsmLn;
  70. procedure AsmCreate(Aplace:tcutplace);
  71. procedure AsmClose;
  72. procedure Synchronize;
  73. public
  74. Constructor Create(smart:boolean);
  75. end;
  76. Procedure GenerateAsm(smart:boolean);
  77. Procedure OnlyAsm;
  78. Implementation
  79. uses
  80. cutils,script,fmodule,systems,verbose
  81. {$ifdef unix}
  82. {$ifdef ver1_0}
  83. ,linux
  84. {$else}
  85. ,unix
  86. {$endif}
  87. {$endif}
  88. {$ifdef i386}
  89. {$ifndef NoAg386Bin}
  90. ,ag386bin
  91. {$endif}
  92. {$ifndef NoAg386Att}
  93. ,ag386att
  94. {$endif NoAg386Att}
  95. {$ifndef NoAg386Nsm}
  96. ,ag386nsm
  97. {$endif NoAg386Nsm}
  98. {$ifndef NoAg386Int}
  99. ,ag386int
  100. {$endif NoAg386Int}
  101. {$ifdef Ag386Cof}
  102. ,ag386cof
  103. {$endif Ag386Cof}
  104. {$endif}
  105. {$ifdef m68k}
  106. {$ifndef NoAg68kGas}
  107. ,ag68kgas
  108. {$endif NoAg68kGas}
  109. {$ifndef NoAg68kMot}
  110. ,ag68kmot
  111. {$endif NoAg68kMot}
  112. {$ifndef NoAg68kMit}
  113. ,ag68kmit
  114. {$endif NoAg68kMit}
  115. {$ifndef NoAg68kMpw}
  116. ,ag68kmpw
  117. {$endif NoAg68kMpw}
  118. {$endif}
  119. ;
  120. {*****************************************************************************
  121. TAssembler
  122. *****************************************************************************}
  123. Constructor TAssembler.Create(smart:boolean);
  124. begin
  125. { load start values }
  126. asmfile:=current_module.asmfilename^;
  127. objfile:=current_module.objfilename^;
  128. name:=FixFileName(current_module.modulename^);
  129. SmartAsm:=smart;
  130. SmartFilesCount:=0;
  131. SmartHeaderCount:=0;
  132. SmartLinkOFiles.Clear;
  133. { Which path will be used ? }
  134. if SmartAsm then
  135. begin
  136. path:=current_module.outputpath^+FixFileName(current_module.modulename^)+target_info.smartext;
  137. CreateSmartLinkPath(path);
  138. path:=FixPath(path,false);
  139. end
  140. else
  141. path:=current_module.outputpath^;
  142. end;
  143. Destructor TAssembler.Destroy;
  144. begin
  145. end;
  146. procedure TAssembler.WriteTree(p:TAAsmoutput);
  147. begin
  148. end;
  149. procedure TAssembler.WriteAsmList;
  150. begin
  151. end;
  152. procedure TAssembler.CreateSmartLinkPath(const s:string);
  153. var
  154. dir : searchrec;
  155. begin
  156. if PathExists(s) then
  157. begin
  158. { the path exists, now we clean only all the .o and .s files }
  159. { .o files }
  160. findfirst(s+dirsep+'*'+target_info.objext,anyfile,dir);
  161. while (doserror=0) do
  162. begin
  163. RemoveFile(s+dirsep+dir.name);
  164. findnext(dir);
  165. end;
  166. findclose(dir);
  167. { .s files }
  168. findfirst(s+dirsep+'*'+target_info.asmext,anyfile,dir);
  169. while (doserror=0) do
  170. begin
  171. RemoveFile(s+dirsep+dir.name);
  172. findnext(dir);
  173. end;
  174. findclose(dir);
  175. end
  176. else
  177. begin
  178. {$I-}
  179. mkdir(s);
  180. {$I+}
  181. if ioresult<>0 then;
  182. end;
  183. end;
  184. procedure TAssembler.NextSmartName(place:tcutplace);
  185. var
  186. s : string;
  187. begin
  188. inc(SmartFilesCount);
  189. if SmartFilesCount>999999 then
  190. Message(asmw_f_too_many_asm_files);
  191. case place of
  192. cut_begin :
  193. begin
  194. inc(SmartHeaderCount);
  195. s:=current_module.asmprefix^+tostr(SmartHeaderCount)+'h';
  196. end;
  197. cut_normal :
  198. s:=current_module.asmprefix^+tostr(SmartHeaderCount)+'s';
  199. cut_end :
  200. s:=current_module.asmprefix^+tostr(SmartHeaderCount)+'t';
  201. end;
  202. AsmFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  203. ObjFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  204. { insert in container so it can be cleared after the linking }
  205. SmartLinkOFiles.Insert(Objfile);
  206. end;
  207. {*****************************************************************************
  208. TExternalAssembler
  209. *****************************************************************************}
  210. Function DoPipe:boolean;
  211. begin
  212. DoPipe:=(cs_asm_pipe in aktglobalswitches) and
  213. not(cs_asm_leave in aktglobalswitches)
  214. {$ifdef i386}
  215. and (aktoutputformat=as_i386_as)
  216. {$endif i386}
  217. {$ifdef m68k}
  218. and (aktoutputformat=as_m68k_as);
  219. {$endif m68k}
  220. end;
  221. Constructor TExternalAssembler.Create(smart:boolean);
  222. begin
  223. inherited Create(smart);
  224. Outcnt:=0;
  225. end;
  226. const
  227. lastas : byte=255;
  228. var
  229. LastASBin : pathstr;
  230. Function TExternalAssembler.FindAssembler:string;
  231. var
  232. asfound : boolean;
  233. UtilExe : string;
  234. begin
  235. asfound:=false;
  236. UtilExe:=AddExtension(target_asm.asmbin,source_os.exeext);
  237. if lastas<>ord(target_asm.id) then
  238. begin
  239. lastas:=ord(target_asm.id);
  240. { is an assembler passed ? }
  241. if utilsdirectory<>'' then
  242. asfound:=FindFile(UtilExe,utilsdirectory,LastASBin);
  243. if not AsFound then
  244. asfound:=FindExe(UtilExe,LastASBin);
  245. if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
  246. begin
  247. Message1(exec_w_assembler_not_found,LastASBin);
  248. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  249. end;
  250. if asfound then
  251. Message1(exec_t_using_assembler,LastASBin);
  252. end;
  253. FindAssembler:=LastASBin;
  254. end;
  255. Function TExternalAssembler.CallAssembler(const command,para:string):Boolean;
  256. begin
  257. callassembler:=true;
  258. if not(cs_asm_extern in aktglobalswitches) then
  259. begin
  260. swapvectors;
  261. exec(command,para);
  262. swapvectors;
  263. if (doserror<>0) then
  264. begin
  265. Message1(exec_w_cant_call_assembler,tostr(doserror));
  266. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  267. callassembler:=false;
  268. end
  269. else
  270. if (dosexitcode<>0) then
  271. begin
  272. Message1(exec_w_error_while_assembling,tostr(dosexitcode));
  273. callassembler:=false;
  274. end;
  275. end
  276. else
  277. AsmRes.AddAsmCommand(command,para,name);
  278. end;
  279. procedure TExternalAssembler.RemoveAsm;
  280. var
  281. g : file;
  282. begin
  283. if cs_asm_leave in aktglobalswitches then
  284. exit;
  285. if cs_asm_extern in aktglobalswitches then
  286. AsmRes.AddDeleteCommand(AsmFile)
  287. else
  288. begin
  289. assign(g,AsmFile);
  290. {$I-}
  291. erase(g);
  292. {$I+}
  293. if ioresult<>0 then;
  294. end;
  295. end;
  296. Function TExternalAssembler.DoAssemble:boolean;
  297. var
  298. s : string;
  299. begin
  300. DoAssemble:=true;
  301. if DoPipe then
  302. exit;
  303. if not(cs_asm_extern in aktglobalswitches) then
  304. begin
  305. if SmartAsm then
  306. begin
  307. if (SmartFilesCount<=1) then
  308. Message1(exec_i_assembling_smart,name);
  309. end
  310. else
  311. Message1(exec_i_assembling,name);
  312. end;
  313. s:=target_asm.asmcmd;
  314. Replace(s,'$ASM',AsmFile);
  315. Replace(s,'$OBJ',ObjFile);
  316. if CallAssembler(FindAssembler,s) then
  317. RemoveAsm
  318. else
  319. begin
  320. DoAssemble:=false;
  321. GenerateError;
  322. end;
  323. end;
  324. Procedure TExternalAssembler.AsmFlush;
  325. begin
  326. if outcnt>0 then
  327. begin
  328. BlockWrite(outfile,outbuf,outcnt);
  329. outcnt:=0;
  330. end;
  331. end;
  332. Procedure TExternalAssembler.AsmClear;
  333. begin
  334. outcnt:=0;
  335. end;
  336. Procedure TExternalAssembler.AsmWrite(const s:string);
  337. begin
  338. if OutCnt+length(s)>=AsmOutSize then
  339. AsmFlush;
  340. Move(s[1],OutBuf[OutCnt],length(s));
  341. inc(OutCnt,length(s));
  342. inc(AsmSize,length(s));
  343. end;
  344. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  345. begin
  346. AsmWrite(s);
  347. AsmLn;
  348. end;
  349. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  350. var
  351. i,j : longint;
  352. begin
  353. i:=StrLen(p);
  354. j:=i;
  355. while j>0 do
  356. begin
  357. i:=min(j,AsmOutSize);
  358. if OutCnt+i>=AsmOutSize then
  359. AsmFlush;
  360. Move(p[0],OutBuf[OutCnt],i);
  361. inc(OutCnt,i);
  362. inc(AsmSize,i);
  363. dec(j,i);
  364. p:=pchar(@p[i]);
  365. end;
  366. end;
  367. Procedure TExternalAssembler.AsmLn;
  368. begin
  369. if OutCnt>=AsmOutSize-2 then
  370. AsmFlush;
  371. OutBuf[OutCnt]:=target_os.newline[1];
  372. inc(OutCnt);
  373. inc(AsmSize);
  374. if length(target_os.newline)>1 then
  375. begin
  376. OutBuf[OutCnt]:=target_os.newline[2];
  377. inc(OutCnt);
  378. inc(AsmSize);
  379. end;
  380. end;
  381. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  382. begin
  383. if SmartAsm then
  384. NextSmartName(Aplace);
  385. {$ifdef unix}
  386. if DoPipe then
  387. begin
  388. Message1(exec_i_assembling_pipe,asmfile);
  389. POpen(outfile,'as -o '+objfile,'W');
  390. end
  391. else
  392. {$endif}
  393. begin
  394. Assign(outfile,asmfile);
  395. {$I-}
  396. Rewrite(outfile,1);
  397. {$I+}
  398. if ioresult<>0 then
  399. Message1(exec_d_cant_create_asmfile,asmfile);
  400. end;
  401. outcnt:=0;
  402. AsmSize:=0;
  403. AsmStartSize:=0;
  404. end;
  405. procedure TExternalAssembler.AsmClose;
  406. var
  407. f : file;
  408. l : longint;
  409. begin
  410. AsmFlush;
  411. {$ifdef unix}
  412. if DoPipe then
  413. PClose(outfile)
  414. else
  415. {$endif}
  416. begin
  417. {Touch Assembler time to ppu time is there is a ppufilename}
  418. if Assigned(current_module.ppufilename) then
  419. begin
  420. Assign(f,current_module.ppufilename^);
  421. {$I-}
  422. reset(f,1);
  423. {$I+}
  424. if ioresult=0 then
  425. begin
  426. getftime(f,l);
  427. close(f);
  428. reset(outfile,1);
  429. setftime(outfile,l);
  430. end;
  431. end;
  432. close(outfile);
  433. end;
  434. end;
  435. {Touch Assembler and object time to ppu time is there is a ppufilename}
  436. procedure TExternalAssembler.Synchronize;
  437. begin
  438. {Touch Assembler time to ppu time is there is a ppufilename}
  439. if Assigned(current_module.ppufilename) then
  440. begin
  441. SynchronizeFileTime(current_module.ppufilename^,asmfile);
  442. if not(cs_asm_extern in aktglobalswitches) then
  443. SynchronizeFileTime(current_module.ppufilename^,objfile);
  444. end;
  445. end;
  446. {*****************************************************************************
  447. Generate Assembler Files Main Procedure
  448. *****************************************************************************}
  449. Procedure GenerateAsm(smart:boolean);
  450. var
  451. a : TExternalAssembler;
  452. {$ifdef i386}
  453. {$ifndef NoAg386Bin}
  454. b : TInternalAssembler;
  455. {$endif}
  456. {$endif}
  457. begin
  458. case aktoutputformat of
  459. as_none : ;
  460. {$ifdef i386}
  461. {$ifndef NoAg386Bin}
  462. as_i386_dbg,
  463. as_i386_coff,
  464. as_i386_pecoff,
  465. as_i386_elf :
  466. begin
  467. case aktoutputformat of
  468. as_i386_dbg :
  469. b:=TInternalAssembler.Create(og_dbg,smart);
  470. as_i386_coff :
  471. b:=TInternalAssembler.Create(og_coff,smart);
  472. as_i386_pecoff :
  473. b:=TInternalAssembler.Create(og_pecoff,smart);
  474. as_i386_elf :
  475. b:=TInternalAssembler.Create(og_elf,smart);
  476. end;
  477. b.WriteBin;
  478. b.Free;
  479. if assigned(current_module.ppufilename) then
  480. begin
  481. if smart then
  482. SynchronizeFileTime(current_module.ppufilename^,current_module.staticlibfilename^)
  483. else
  484. SynchronizeFileTime(current_module.ppufilename^,current_module.objfilename^);
  485. end;
  486. exit;
  487. end;
  488. {$endif NoAg386Bin}
  489. {$ifndef NoAg386Att}
  490. as_i386_as,
  491. as_i386_as_aout,
  492. as_i386_asw :
  493. a:=T386ATTAssembler.create(smart);
  494. {$endif NoAg386Att}
  495. {$ifndef NoAg386Nsm}
  496. as_i386_nasmcoff,
  497. as_i386_nasmwin32,
  498. as_i386_nasmelf,
  499. as_i386_nasmobj :
  500. a:=T386NasmAssembler.Create(smart);
  501. {$endif NoAg386Nsm}
  502. {$ifndef NoAg386Int}
  503. as_i386_masm,
  504. as_i386_tasm :
  505. a:=T386IntelAssembler.Create(smart);
  506. {$endif NoAg386Int}
  507. {$endif}
  508. {$ifdef m68k}
  509. {$ifndef NoAg68kGas}
  510. as_m68k_as,
  511. as_m68k_gas :
  512. a:=new(pm68kgasasmlist,Init(smart));
  513. {$endif NoAg86KGas}
  514. {$ifndef NoAg68kMot}
  515. as_m68k_mot :
  516. a:=new(pm68kmoTExternalAssembler,Init(smart));
  517. {$endif NoAg86kMot}
  518. {$ifndef NoAg68kMit}
  519. as_m68k_mit :
  520. a:=new(pm68kmiTExternalAssembler,Init(smart));
  521. {$endif NoAg86KMot}
  522. {$ifndef NoAg68kMpw}
  523. as_m68k_mpw :
  524. a:=new(pm68kmpwasmlist,Init(smart));
  525. {$endif NoAg68kMpw}
  526. {$endif}
  527. else
  528. Message(asmw_f_assembler_output_not_supported);
  529. end;
  530. a.AsmCreate(cut_normal);
  531. a.WriteAsmList;
  532. a.AsmClose;
  533. a.DoAssemble;
  534. a.synchronize;
  535. a.Free;
  536. end;
  537. Procedure OnlyAsm;
  538. var
  539. a : TExternalAssembler;
  540. begin
  541. a:=TExternalAssembler.Create(false);
  542. a.DoAssemble;
  543. a.Free;
  544. end;
  545. end.
  546. {
  547. $Log$
  548. Revision 1.15 2001-03-05 21:39:11 peter
  549. * changed to class with common TAssembler also for internal assembler
  550. Revision 1.14 2001/02/26 08:08:16 michael
  551. * bug correction: pipes must be closed by pclose (not close);
  552. There was too many not closed processes under Linux before patch.
  553. Test this by making a compiler under Linux with command
  554. OPT="-P" make
  555. and check a list of processes in another shell with
  556. ps -xa
  557. Revision 1.13 2001/02/20 21:36:39 peter
  558. * tasm/masm fixes merged
  559. Revision 1.12 2001/02/09 23:06:17 peter
  560. * fixed uninited var
  561. Revision 1.11 2001/02/05 20:46:59 peter
  562. * support linux unit for ver1_0 compilers
  563. Revision 1.10 2001/01/21 20:32:45 marco
  564. * Renamefest. Compiler part. Not that hard.
  565. Revision 1.9 2001/01/12 19:19:44 peter
  566. * fixed searching for utils
  567. Revision 1.8 2000/12/25 00:07:25 peter
  568. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  569. tlinkedlist objects)
  570. Revision 1.7 2000/11/13 15:26:12 marco
  571. * Renamefest
  572. Revision 1.6 2000/10/01 19:48:23 peter
  573. * lot of compile updates for cg11
  574. Revision 1.5 2000/09/24 15:06:11 peter
  575. * use defines.inc
  576. Revision 1.4 2000/08/27 16:11:49 peter
  577. * moved some util functions from globals,cobjects to cutils
  578. * splitted files into finput,fmodule
  579. Revision 1.3 2000/07/13 12:08:24 michael
  580. + patched to 1.1.0 with former 1.09patch from peter
  581. Revision 1.2 2000/07/13 11:32:32 michael
  582. + removed logs
  583. }