assemble.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. unit assemble;
  18. interface
  19. uses
  20. {$ifdef Delphi}
  21. dmisc,
  22. {$else Delphi}
  23. dos,
  24. {$endif Delphi}
  25. cobjects,globtype,globals,aasm;
  26. const
  27. {$ifdef tp}
  28. AsmOutSize=1024;
  29. {$else}
  30. AsmOutSize=32768;
  31. {$endif}
  32. type
  33. PAsmList=^TAsmList;
  34. TAsmList=object
  35. {filenames}
  36. path : pathstr;
  37. name : namestr;
  38. asmfile, { current .s and .o file }
  39. objfile,
  40. as_bin : string;
  41. SmartAsm : boolean;
  42. smarthcount : longint;
  43. place : TCutPlace; { special 'end' file for import dir ? }
  44. {outfile}
  45. AsmSize,
  46. AsmStartSize,
  47. outcnt : longint;
  48. outbuf : array[0..AsmOutSize-1] of char;
  49. outfile : file;
  50. Constructor Init(smart:boolean);
  51. Destructor Done;
  52. Function FindAssembler:string;
  53. Function CallAssembler(const command,para:string):Boolean;
  54. Function DoAssemble:boolean;
  55. Procedure RemoveAsm;
  56. procedure NextSmartName;
  57. Procedure AsmFlush;
  58. Procedure AsmClear;
  59. Procedure AsmWrite(const s:string);
  60. Procedure AsmWritePChar(p:pchar);
  61. Procedure AsmWriteLn(const s:string);
  62. Procedure AsmLn;
  63. procedure AsmCreate(Aplace:tcutplace);
  64. procedure AsmClose;
  65. procedure Synchronize;
  66. procedure WriteTree(p:paasmoutput);virtual;
  67. procedure WriteAsmList;virtual;
  68. end;
  69. var
  70. SmartLinkFilesCnt : longint;
  71. Procedure GenerateAsm(smart:boolean);
  72. Procedure OnlyAsm;
  73. Implementation
  74. uses
  75. script,files,systems,verbose
  76. {$ifdef linux}
  77. ,linux
  78. {$endif}
  79. ,strings
  80. {$ifdef i386}
  81. {$ifndef NoAg386Bin}
  82. ,ag386bin
  83. {$endif}
  84. {$ifndef NoAg386Att}
  85. ,ag386att
  86. {$endif NoAg386Att}
  87. {$ifndef NoAg386Nsm}
  88. ,ag386nsm
  89. {$endif NoAg386Nsm}
  90. {$ifndef NoAg386Int}
  91. ,ag386int
  92. {$endif NoAg386Int}
  93. {$ifdef Ag386Cof}
  94. ,ag386cof
  95. {$endif Ag386Cof}
  96. {$endif}
  97. {$ifdef m68k}
  98. {$ifndef NoAg68kGas}
  99. ,ag68kgas
  100. {$endif NoAg68kGas}
  101. {$ifndef NoAg68kMot}
  102. ,ag68kmot
  103. {$endif NoAg68kMot}
  104. {$ifndef NoAg68kMit}
  105. ,ag68kmit
  106. {$endif NoAg68kMit}
  107. {$ifndef NoAg68kMpw}
  108. ,ag68kmpw
  109. {$endif NoAg68kMpw}
  110. {$endif}
  111. ;
  112. {*****************************************************************************
  113. TAsmList
  114. *****************************************************************************}
  115. Function DoPipe:boolean;
  116. begin
  117. DoPipe:=(cs_asm_pipe in aktglobalswitches) and
  118. not(cs_asm_leave in aktglobalswitches)
  119. {$ifdef i386}
  120. and (aktoutputformat=as_i386_as)
  121. {$endif i386}
  122. {$ifdef m68k}
  123. and (aktoutputformat=as_m68k_as);
  124. {$endif m68k}
  125. end;
  126. const
  127. lastas : byte=255;
  128. var
  129. LastASBin : pathstr;
  130. Function TAsmList.FindAssembler:string;
  131. var
  132. asfound : boolean;
  133. begin
  134. if lastas<>ord(target_asm.id) then
  135. begin
  136. lastas:=ord(target_asm.id);
  137. { is an assembler passed ? }
  138. if utilsdirectory<>'' then
  139. LastASBin:=FindFile(target_asm.asmbin+source_os.exeext,utilsdirectory,asfound)+
  140. target_asm.asmbin+source_os.exeext;
  141. if LastASBin='' then
  142. LastASBin:=FindExe(target_asm.asmbin,asfound);
  143. if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
  144. begin
  145. Message1(exec_w_assembler_not_found,LastASBin);
  146. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  147. end;
  148. if asfound then
  149. Message1(exec_t_using_assembler,LastASBin);
  150. end;
  151. FindAssembler:=LastASBin;
  152. end;
  153. Function TAsmList.CallAssembler(const command,para:string):Boolean;
  154. begin
  155. callassembler:=true;
  156. if not(cs_asm_extern in aktglobalswitches) then
  157. begin
  158. swapvectors;
  159. exec(command,para);
  160. swapvectors;
  161. if (doserror<>0) then
  162. begin
  163. Message1(exec_w_cant_call_assembler,tostr(doserror));
  164. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  165. callassembler:=false;
  166. end
  167. else
  168. if (dosexitcode<>0) then
  169. begin
  170. Message1(exec_w_error_while_assembling,tostr(dosexitcode));
  171. callassembler:=false;
  172. end;
  173. end
  174. else
  175. AsmRes.AddAsmCommand(command,para,name);
  176. end;
  177. procedure TAsmList.RemoveAsm;
  178. var
  179. g : file;
  180. begin
  181. if cs_asm_leave in aktglobalswitches then
  182. exit;
  183. if cs_asm_extern in aktglobalswitches then
  184. AsmRes.AddDeleteCommand(AsmFile)
  185. else
  186. begin
  187. assign(g,AsmFile);
  188. {$I-}
  189. erase(g);
  190. {$I+}
  191. if ioresult<>0 then;
  192. end;
  193. end;
  194. Function TAsmList.DoAssemble:boolean;
  195. var
  196. s : string;
  197. begin
  198. DoAssemble:=true;
  199. if DoPipe then
  200. exit;
  201. if not(cs_asm_extern in aktglobalswitches) then
  202. begin
  203. if SmartAsm then
  204. begin
  205. if (SmartLinkFilesCnt<=1) then
  206. Message1(exec_i_assembling_smart,name);
  207. end
  208. else
  209. Message1(exec_i_assembling,name);
  210. end;
  211. s:=target_asm.asmcmd;
  212. Replace(s,'$ASM',AsmFile);
  213. Replace(s,'$OBJ',ObjFile);
  214. if CallAssembler(FindAssembler,s) then
  215. RemoveAsm
  216. else
  217. begin
  218. DoAssemble:=false;
  219. GenerateError;
  220. end;
  221. end;
  222. procedure TAsmList.NextSmartName;
  223. var
  224. s : string;
  225. begin
  226. inc(SmartLinkFilesCnt);
  227. if SmartLinkFilesCnt>999999 then
  228. Message(asmw_f_too_many_asm_files);
  229. case place of
  230. cut_begin :
  231. begin
  232. inc(smarthcount);
  233. s:=current_module^.asmprefix^+tostr(smarthcount)+'h';
  234. end;
  235. cut_normal :
  236. s:=current_module^.asmprefix^+tostr(smarthcount)+'s';
  237. cut_end :
  238. s:=current_module^.asmprefix^+tostr(smarthcount)+'t';
  239. end;
  240. AsmFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.asmext);
  241. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext);
  242. { insert in container so it can be cleared after the linking }
  243. SmartLinkOFiles.Insert(Objfile);
  244. end;
  245. {*****************************************************************************
  246. TAsmList AsmFile Writing
  247. *****************************************************************************}
  248. Procedure TAsmList.AsmFlush;
  249. begin
  250. if outcnt>0 then
  251. begin
  252. BlockWrite(outfile,outbuf,outcnt);
  253. outcnt:=0;
  254. end;
  255. end;
  256. Procedure TAsmList.AsmClear;
  257. begin
  258. outcnt:=0;
  259. end;
  260. Procedure TAsmList.AsmWrite(const s:string);
  261. begin
  262. if OutCnt+length(s)>=AsmOutSize then
  263. AsmFlush;
  264. Move(s[1],OutBuf[OutCnt],length(s));
  265. inc(OutCnt,length(s));
  266. inc(AsmSize,length(s));
  267. end;
  268. Procedure TAsmList.AsmWriteLn(const s:string);
  269. begin
  270. AsmWrite(s);
  271. AsmLn;
  272. end;
  273. Procedure TAsmList.AsmWritePChar(p:pchar);
  274. var
  275. i,j : longint;
  276. begin
  277. i:=StrLen(p);
  278. j:=i;
  279. while j>0 do
  280. begin
  281. i:=min(j,AsmOutSize);
  282. if OutCnt+i>=AsmOutSize then
  283. AsmFlush;
  284. Move(p[0],OutBuf[OutCnt],i);
  285. inc(OutCnt,i);
  286. inc(AsmSize,i);
  287. dec(j,i);
  288. p:=pchar(@p[i]);
  289. end;
  290. end;
  291. Procedure TAsmList.AsmLn;
  292. begin
  293. if OutCnt>=AsmOutSize-2 then
  294. AsmFlush;
  295. OutBuf[OutCnt]:=target_os.newline[1];
  296. inc(OutCnt);
  297. inc(AsmSize);
  298. if length(target_os.newline)>1 then
  299. begin
  300. OutBuf[OutCnt]:=target_os.newline[2];
  301. inc(OutCnt);
  302. inc(AsmSize);
  303. end;
  304. end;
  305. procedure TAsmList.AsmCreate(Aplace:tcutplace);
  306. begin
  307. place:=Aplace;
  308. if SmartAsm then
  309. NextSmartName;
  310. {$ifdef linux}
  311. if DoPipe then
  312. begin
  313. Message1(exec_i_assembling_pipe,asmfile);
  314. POpen(outfile,'as -o '+objfile,'W');
  315. end
  316. else
  317. {$endif}
  318. begin
  319. Assign(outfile,asmfile);
  320. {$I-}
  321. Rewrite(outfile,1);
  322. {$I+}
  323. if ioresult<>0 then
  324. Message1(exec_d_cant_create_asmfile,asmfile);
  325. end;
  326. outcnt:=0;
  327. AsmSize:=0;
  328. AsmStartSize:=0;
  329. end;
  330. procedure TAsmList.AsmClose;
  331. var
  332. f : file;
  333. l : longint;
  334. begin
  335. AsmFlush;
  336. {$ifdef linux}
  337. if DoPipe then
  338. Close(outfile)
  339. else
  340. {$endif}
  341. begin
  342. {Touch Assembler time to ppu time is there is a ppufilename}
  343. if Assigned(current_module^.ppufilename) then
  344. begin
  345. Assign(f,current_module^.ppufilename^);
  346. {$I-}
  347. reset(f,1);
  348. {$I+}
  349. if ioresult=0 then
  350. begin
  351. getftime(f,l);
  352. close(f);
  353. reset(outfile,1);
  354. setftime(outfile,l);
  355. end;
  356. end;
  357. close(outfile);
  358. end;
  359. end;
  360. {Touch Assembler and object time to ppu time is there is a ppufilename}
  361. procedure TAsmList.Synchronize;
  362. begin
  363. {Touch Assembler time to ppu time is there is a ppufilename}
  364. if Assigned(current_module^.ppufilename) then
  365. begin
  366. SynchronizeFileTime(current_module^.ppufilename^,asmfile);
  367. if not(cs_asm_extern in aktglobalswitches) then
  368. SynchronizeFileTime(current_module^.ppufilename^,objfile);
  369. end;
  370. end;
  371. procedure TAsmList.WriteTree(p:paasmoutput);
  372. begin
  373. end;
  374. procedure TAsmList.WriteAsmList;
  375. begin
  376. end;
  377. Constructor TAsmList.Init(smart:boolean);
  378. begin
  379. { load start values }
  380. asmfile:=current_module^.asmfilename^;
  381. objfile:=current_module^.objfilename^;
  382. name:=FixFileName(current_module^.modulename^);
  383. OutCnt:=0;
  384. SmartLinkFilesCnt:=0;
  385. SmartLinkOFiles.Clear;
  386. place:=cut_normal;
  387. SmartAsm:=smart;
  388. SmartHCount:=0;
  389. { Which path will be used ? }
  390. if SmartAsm then
  391. begin
  392. path:=current_module^.outputpath^+FixFileName(current_module^.modulename^)+target_info.smartext;
  393. {$I-}
  394. mkdir(path);
  395. {$I+}
  396. if ioresult<>0 then;
  397. path:=FixPath(path,false);
  398. end
  399. else
  400. path:=current_module^.outputpath^;
  401. end;
  402. Destructor TAsmList.Done;
  403. begin
  404. end;
  405. {*****************************************************************************
  406. Generate Assembler Files Main Procedure
  407. *****************************************************************************}
  408. Procedure GenerateAsm(smart:boolean);
  409. var
  410. a : PAsmList;
  411. {$ifdef i386}
  412. {$ifndef NoAg386Bin}
  413. b : Pi386binasmlist;
  414. {$endif}
  415. {$endif}
  416. begin
  417. case aktoutputformat of
  418. as_none : ;
  419. {$ifdef i386}
  420. {$ifndef NoAg386Bin}
  421. as_i386_dbg,
  422. as_i386_coff,
  423. as_i386_pecoff :
  424. begin
  425. case aktoutputformat of
  426. as_i386_dbg :
  427. b:=new(pi386binasmlist,Init(og_dbg,smart));
  428. as_i386_coff :
  429. b:=new(pi386binasmlist,Init(og_coff,smart));
  430. as_i386_pecoff :
  431. b:=new(pi386binasmlist,Init(og_pecoff,smart));
  432. end;
  433. b^.WriteBin;
  434. dispose(b,done);
  435. if assigned(current_module^.ppufilename) then
  436. begin
  437. if smart then
  438. SynchronizeFileTime(current_module^.ppufilename^,current_module^.staticlibfilename^)
  439. else
  440. SynchronizeFileTime(current_module^.ppufilename^,current_module^.objfilename^);
  441. end;
  442. exit;
  443. end;
  444. {$endif NoAg386Bin}
  445. {$ifndef NoAg386Att}
  446. as_i386_as,
  447. as_i386_as_aout,
  448. as_i386_asw :
  449. a:=new(pi386attasmlist,Init(smart));
  450. {$endif NoAg386Att}
  451. {$ifndef NoAg386Nsm}
  452. as_i386_nasmcoff,
  453. as_i386_nasmelf,
  454. as_i386_nasmobj :
  455. a:=new(pi386nasmasmlist,Init(smart));
  456. {$endif NoAg386Nsm}
  457. {$ifndef NoAg386Int}
  458. as_i386_tasm :
  459. a:=new(pi386intasmlist,Init(smart));
  460. {$endif NoAg386Int}
  461. {$endif}
  462. {$ifdef m68k}
  463. {$ifndef NoAg68kGas}
  464. as_m68k_as,
  465. as_m68k_gas :
  466. a:=new(pm68kgasasmlist,Init(smart));
  467. {$endif NoAg86KGas}
  468. {$ifndef NoAg68kMot}
  469. as_m68k_mot :
  470. a:=new(pm68kmotasmlist,Init(smart));
  471. {$endif NoAg86kMot}
  472. {$ifndef NoAg68kMit}
  473. as_m68k_mit :
  474. a:=new(pm68kmitasmlist,Init(smart));
  475. {$endif NoAg86KMot}
  476. {$ifndef NoAg68kMpw}
  477. as_m68k_mpw :
  478. a:=new(pm68kmpwasmlist,Init(smart));
  479. {$endif NoAg68kMpw}
  480. {$endif}
  481. else
  482. {$ifdef TP}
  483. exit;
  484. {$else}
  485. Message(asmw_f_assembler_output_not_supported);
  486. {$endif}
  487. end;
  488. a^.AsmCreate(cut_normal);
  489. a^.WriteAsmList;
  490. a^.AsmClose;
  491. a^.DoAssemble;
  492. a^.synchronize;
  493. dispose(a,Done);
  494. end;
  495. Procedure OnlyAsm;
  496. var
  497. a : PAsmList;
  498. begin
  499. a:=new(pasmlist,Init(false));
  500. a^.DoAssemble;
  501. dispose(a,Done);
  502. end;
  503. end.
  504. {
  505. $Log$
  506. Revision 1.62 2000-02-24 18:41:38 peter
  507. * removed warnings/notes
  508. Revision 1.61 2000/02/09 13:22:45 peter
  509. * log truncated
  510. Revision 1.60 2000/01/11 09:52:06 peter
  511. * fixed placing of .sl directories
  512. * use -b again for base-file selection
  513. * fixed group writing for linux with smartlinking
  514. Revision 1.59 2000/01/07 01:14:19 peter
  515. * updated copyright to 2000
  516. Revision 1.58 1999/11/12 11:03:49 peter
  517. * searchpaths changed to stringqueue object
  518. Revision 1.57 1999/11/08 10:37:12 peter
  519. * filename fixes for win32 imports for units with multiple needed dll's
  520. Revision 1.56 1999/11/06 14:34:17 peter
  521. * truncated log to 20 revs
  522. Revision 1.55 1999/11/02 15:06:57 peter
  523. * import library fixes for win32
  524. * alignment works again
  525. Revision 1.54 1999/09/16 11:34:44 pierre
  526. * typo correction
  527. Revision 1.53 1999/09/02 18:47:44 daniel
  528. * Could not compile with TP, some arrays moved to heap
  529. * NOAG386BIN default for TP
  530. * AG386* files were not compatible with TP, fixed.
  531. }