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. i : word;
  181. begin
  182. if cs_asm_leave in aktglobalswitches then
  183. exit;
  184. if cs_asm_extern in aktglobalswitches then
  185. AsmRes.AddDeleteCommand(AsmFile)
  186. else
  187. begin
  188. assign(g,AsmFile);
  189. {$I-}
  190. erase(g);
  191. {$I+}
  192. i:=ioresult;
  193. end;
  194. end;
  195. Function TAsmList.DoAssemble:boolean;
  196. var
  197. s : string;
  198. begin
  199. DoAssemble:=true;
  200. if DoPipe then
  201. exit;
  202. if not(cs_asm_extern in aktglobalswitches) then
  203. begin
  204. if SmartAsm then
  205. begin
  206. if (SmartLinkFilesCnt<=1) then
  207. Message1(exec_i_assembling_smart,name);
  208. end
  209. else
  210. Message1(exec_i_assembling,name);
  211. end;
  212. s:=target_asm.asmcmd;
  213. Replace(s,'$ASM',AsmFile);
  214. Replace(s,'$OBJ',ObjFile);
  215. if CallAssembler(FindAssembler,s) then
  216. RemoveAsm
  217. else
  218. begin
  219. DoAssemble:=false;
  220. GenerateError;
  221. end;
  222. end;
  223. procedure TAsmList.NextSmartName;
  224. var
  225. s : string;
  226. begin
  227. inc(SmartLinkFilesCnt);
  228. if SmartLinkFilesCnt>999999 then
  229. Message(asmw_f_too_many_asm_files);
  230. case place of
  231. cut_begin :
  232. begin
  233. inc(smarthcount);
  234. s:=current_module^.asmprefix^+tostr(smarthcount)+'h';
  235. end;
  236. cut_normal :
  237. s:=current_module^.asmprefix^+tostr(smarthcount)+'s';
  238. cut_end :
  239. s:=current_module^.asmprefix^+tostr(smarthcount)+'t';
  240. end;
  241. AsmFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.asmext);
  242. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext);
  243. { insert in container so it can be cleared after the linking }
  244. SmartLinkOFiles.Insert(Objfile);
  245. end;
  246. {*****************************************************************************
  247. TAsmList AsmFile Writing
  248. *****************************************************************************}
  249. Procedure TAsmList.AsmFlush;
  250. begin
  251. if outcnt>0 then
  252. begin
  253. BlockWrite(outfile,outbuf,outcnt);
  254. outcnt:=0;
  255. end;
  256. end;
  257. Procedure TAsmList.AsmClear;
  258. begin
  259. outcnt:=0;
  260. end;
  261. Procedure TAsmList.AsmWrite(const s:string);
  262. begin
  263. if OutCnt+length(s)>=AsmOutSize then
  264. AsmFlush;
  265. Move(s[1],OutBuf[OutCnt],length(s));
  266. inc(OutCnt,length(s));
  267. inc(AsmSize,length(s));
  268. end;
  269. Procedure TAsmList.AsmWriteLn(const s:string);
  270. begin
  271. AsmWrite(s);
  272. AsmLn;
  273. end;
  274. Procedure TAsmList.AsmWritePChar(p:pchar);
  275. var
  276. i,j : longint;
  277. begin
  278. i:=StrLen(p);
  279. j:=i;
  280. while j>0 do
  281. begin
  282. i:=min(j,AsmOutSize);
  283. if OutCnt+i>=AsmOutSize then
  284. AsmFlush;
  285. Move(p[0],OutBuf[OutCnt],i);
  286. inc(OutCnt,i);
  287. inc(AsmSize,i);
  288. dec(j,i);
  289. p:=pchar(@p[i]);
  290. end;
  291. end;
  292. Procedure TAsmList.AsmLn;
  293. begin
  294. if OutCnt>=AsmOutSize-2 then
  295. AsmFlush;
  296. OutBuf[OutCnt]:=target_os.newline[1];
  297. inc(OutCnt);
  298. inc(AsmSize);
  299. if length(target_os.newline)>1 then
  300. begin
  301. OutBuf[OutCnt]:=target_os.newline[2];
  302. inc(OutCnt);
  303. inc(AsmSize);
  304. end;
  305. end;
  306. procedure TAsmList.AsmCreate(Aplace:tcutplace);
  307. begin
  308. place:=Aplace;
  309. if SmartAsm then
  310. NextSmartName;
  311. {$ifdef linux}
  312. if DoPipe then
  313. begin
  314. Message1(exec_i_assembling_pipe,asmfile);
  315. POpen(outfile,'as -o '+objfile,'W');
  316. end
  317. else
  318. {$endif}
  319. begin
  320. Assign(outfile,asmfile);
  321. {$I-}
  322. Rewrite(outfile,1);
  323. {$I+}
  324. if ioresult<>0 then
  325. Message1(exec_d_cant_create_asmfile,asmfile);
  326. end;
  327. outcnt:=0;
  328. AsmSize:=0;
  329. AsmStartSize:=0;
  330. end;
  331. procedure TAsmList.AsmClose;
  332. var
  333. f : file;
  334. l : longint;
  335. begin
  336. AsmFlush;
  337. {$ifdef linux}
  338. if DoPipe then
  339. Close(outfile)
  340. else
  341. {$endif}
  342. begin
  343. {Touch Assembler time to ppu time is there is a ppufilename}
  344. if Assigned(current_module^.ppufilename) then
  345. begin
  346. Assign(f,current_module^.ppufilename^);
  347. {$I-}
  348. reset(f,1);
  349. {$I+}
  350. if ioresult=0 then
  351. begin
  352. getftime(f,l);
  353. close(f);
  354. reset(outfile,1);
  355. setftime(outfile,l);
  356. end;
  357. end;
  358. close(outfile);
  359. end;
  360. end;
  361. {Touch Assembler and object time to ppu time is there is a ppufilename}
  362. procedure TAsmList.Synchronize;
  363. begin
  364. {Touch Assembler time to ppu time is there is a ppufilename}
  365. if Assigned(current_module^.ppufilename) then
  366. begin
  367. SynchronizeFileTime(current_module^.ppufilename^,asmfile);
  368. if not(cs_asm_extern in aktglobalswitches) then
  369. SynchronizeFileTime(current_module^.ppufilename^,objfile);
  370. end;
  371. end;
  372. procedure TAsmList.WriteTree(p:paasmoutput);
  373. begin
  374. end;
  375. procedure TAsmList.WriteAsmList;
  376. begin
  377. end;
  378. Constructor TAsmList.Init(smart:boolean);
  379. var
  380. i : word;
  381. begin
  382. { load start values }
  383. asmfile:=current_module^.asmfilename^;
  384. objfile:=current_module^.objfilename^;
  385. name:=FixFileName(current_module^.modulename^);
  386. OutCnt:=0;
  387. SmartLinkFilesCnt:=0;
  388. SmartLinkOFiles.Clear;
  389. place:=cut_normal;
  390. SmartAsm:=smart;
  391. SmartHCount:=0;
  392. { Which path will be used ? }
  393. if SmartAsm then
  394. begin
  395. path:=current_module^.outputpath^+FixFileName(current_module^.modulename^)+target_info.smartext;
  396. {$I-}
  397. mkdir(path);
  398. {$I+}
  399. i:=ioresult;
  400. path:=FixPath(path,false);
  401. end
  402. else
  403. path:=current_module^.outputpath^;
  404. end;
  405. Destructor TAsmList.Done;
  406. begin
  407. end;
  408. {*****************************************************************************
  409. Generate Assembler Files Main Procedure
  410. *****************************************************************************}
  411. Procedure GenerateAsm(smart:boolean);
  412. var
  413. a : PAsmList;
  414. {$ifdef i386}
  415. {$ifndef NoAg386Bin}
  416. b : Pi386binasmlist;
  417. {$endif}
  418. {$endif}
  419. begin
  420. case aktoutputformat of
  421. as_none : ;
  422. {$ifdef i386}
  423. {$ifndef NoAg386Bin}
  424. as_i386_dbg,
  425. as_i386_coff,
  426. as_i386_pecoff :
  427. begin
  428. case aktoutputformat of
  429. as_i386_dbg :
  430. b:=new(pi386binasmlist,Init(og_dbg,smart));
  431. as_i386_coff :
  432. b:=new(pi386binasmlist,Init(og_coff,smart));
  433. as_i386_pecoff :
  434. b:=new(pi386binasmlist,Init(og_pecoff,smart));
  435. end;
  436. b^.WriteBin;
  437. dispose(b,done);
  438. if assigned(current_module^.ppufilename) then
  439. begin
  440. if smart then
  441. SynchronizeFileTime(current_module^.ppufilename^,current_module^.staticlibfilename^)
  442. else
  443. SynchronizeFileTime(current_module^.ppufilename^,current_module^.objfilename^);
  444. end;
  445. exit;
  446. end;
  447. {$endif NoAg386Bin}
  448. {$ifndef NoAg386Att}
  449. as_i386_as,
  450. as_i386_as_aout,
  451. as_i386_asw :
  452. a:=new(pi386attasmlist,Init(smart));
  453. {$endif NoAg386Att}
  454. {$ifndef NoAg386Nsm}
  455. as_i386_nasmcoff,
  456. as_i386_nasmelf,
  457. as_i386_nasmobj :
  458. a:=new(pi386nasmasmlist,Init(smart));
  459. {$endif NoAg386Nsm}
  460. {$ifndef NoAg386Int}
  461. as_i386_tasm :
  462. a:=new(pi386intasmlist,Init(smart));
  463. {$endif NoAg386Int}
  464. {$endif}
  465. {$ifdef m68k}
  466. {$ifndef NoAg68kGas}
  467. as_m68k_as,
  468. as_m68k_gas :
  469. a:=new(pm68kgasasmlist,Init(smart));
  470. {$endif NoAg86KGas}
  471. {$ifndef NoAg68kMot}
  472. as_m68k_mot :
  473. a:=new(pm68kmotasmlist,Init(smart));
  474. {$endif NoAg86kMot}
  475. {$ifndef NoAg68kMit}
  476. as_m68k_mit :
  477. a:=new(pm68kmitasmlist,Init(smart));
  478. {$endif NoAg86KMot}
  479. {$ifndef NoAg68kMpw}
  480. as_m68k_mpw :
  481. a:=new(pm68kmpwasmlist,Init(smart));
  482. {$endif NoAg68kMpw}
  483. {$endif}
  484. else
  485. {$ifdef TP}
  486. exit;
  487. {$else}
  488. Message(asmw_f_assembler_output_not_supported);
  489. {$endif}
  490. end;
  491. a^.AsmCreate(cut_normal);
  492. a^.WriteAsmList;
  493. a^.AsmClose;
  494. a^.DoAssemble;
  495. a^.synchronize;
  496. dispose(a,Done);
  497. end;
  498. Procedure OnlyAsm;
  499. var
  500. a : PAsmList;
  501. begin
  502. a:=new(pasmlist,Init(false));
  503. a^.DoAssemble;
  504. dispose(a,Done);
  505. end;
  506. end.
  507. {
  508. $Log$
  509. Revision 1.61 2000-02-09 13:22:45 peter
  510. * log truncated
  511. Revision 1.60 2000/01/11 09:52:06 peter
  512. * fixed placing of .sl directories
  513. * use -b again for base-file selection
  514. * fixed group writing for linux with smartlinking
  515. Revision 1.59 2000/01/07 01:14:19 peter
  516. * updated copyright to 2000
  517. Revision 1.58 1999/11/12 11:03:49 peter
  518. * searchpaths changed to stringqueue object
  519. Revision 1.57 1999/11/08 10:37:12 peter
  520. * filename fixes for win32 imports for units with multiple needed dll's
  521. Revision 1.56 1999/11/06 14:34:17 peter
  522. * truncated log to 20 revs
  523. Revision 1.55 1999/11/02 15:06:57 peter
  524. * import library fixes for win32
  525. * alignment works again
  526. Revision 1.54 1999/09/16 11:34:44 pierre
  527. * typo correction
  528. Revision 1.53 1999/09/02 18:47:44 daniel
  529. * Could not compile with TP, some arrays moved to heap
  530. * NOAG386BIN default for TP
  531. * AG386* files were not compatible with TP, fixed.
  532. }