assemble.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. {
  2. $Id$
  3. Copyright (c) 1998 by the FPC development team
  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. dos,cobjects,globals,aasm;
  21. const
  22. {$ifdef tp}
  23. AsmOutSize=1024;
  24. {$else}
  25. AsmOutSize=32768;
  26. {$endif}
  27. type
  28. PAsmList=^TAsmList;
  29. TAsmList=object
  30. {filenames}
  31. path : pathstr;
  32. name : namestr;
  33. asmfile, { current .s and .o file }
  34. objfile,
  35. as_bin : string;
  36. IsEndFile : boolean; { special 'end' file for import dir ? }
  37. {outfile}
  38. AsmSize,
  39. AsmStartSize,
  40. outcnt : longint;
  41. outbuf : array[0..AsmOutSize-1] of char;
  42. outfile : file;
  43. Constructor Init;
  44. Destructor Done;
  45. Function FindAssembler:string;
  46. Function CallAssembler(const command,para:string):Boolean;
  47. Function DoAssemble:boolean;
  48. Procedure RemoveAsm;
  49. procedure NextSmartName;
  50. Procedure AsmFlush;
  51. Procedure AsmClear;
  52. Procedure AsmWrite(const s:string);
  53. Procedure AsmWritePChar(p:pchar);
  54. Procedure AsmWriteLn(const s:string);
  55. Procedure AsmLn;
  56. procedure AsmCreate;
  57. procedure AsmClose;
  58. procedure Synchronize;
  59. procedure WriteTree(p:paasmoutput);virtual;
  60. procedure WriteAsmList;virtual;
  61. end;
  62. Procedure GenerateAsm;
  63. Procedure OnlyAsm;
  64. var
  65. SmartLinkFilesCnt : longint;
  66. Implementation
  67. uses
  68. script,files,systems,verbose
  69. {$ifdef linux}
  70. ,linux
  71. {$endif}
  72. ,strings
  73. {$ifdef i386}
  74. {$ifndef NoAg386Att}
  75. ,ag386att
  76. {$endif NoAg386Att}
  77. {$ifndef NoAg386Nsm}
  78. ,ag386nsm
  79. {$endif NoAg386Nsm}
  80. {$ifndef NoAg386Int}
  81. ,ag386int
  82. {$endif NoAg386Int}
  83. {$endif}
  84. {$ifdef m68k}
  85. {$ifndef NoAg68kGas}
  86. ,ag68kgas
  87. {$endif NoAg68kGas}
  88. {$ifndef NoAg68kMot}
  89. ,ag68kmot
  90. {$endif NoAg68kMot}
  91. {$ifndef NoAg68kMit}
  92. ,ag68kmit
  93. {$endif NoAg68kMit}
  94. {$ifndef NoAg68kMpw}
  95. ,ag68kmpw
  96. {$endif NoAg68kMpw}
  97. {$endif}
  98. ;
  99. {*****************************************************************************
  100. TAsmList
  101. *****************************************************************************}
  102. Function DoPipe:boolean;
  103. begin
  104. DoPipe:=(cs_asm_pipe in aktglobalswitches) and
  105. not(cs_asm_leave in aktglobalswitches)
  106. {$ifdef i386}
  107. and (aktoutputformat=as_i386_o)
  108. {$endif i386}
  109. {$ifdef m68k}
  110. and (aktoutputformat=as_m68k_o);
  111. {$endif m68k}
  112. end;
  113. const
  114. lastas : byte=255;
  115. var
  116. LastASBin : string;
  117. Function TAsmList.FindAssembler:string;
  118. var
  119. asfound : boolean;
  120. begin
  121. if lastas<>ord(target_asm.id) then
  122. begin
  123. lastas:=ord(target_asm.id);
  124. { is an assembler passed ? }
  125. if utilsdirectory<>'' then
  126. begin
  127. LastASBin:=Search(target_asm.asmbin+source_os.exeext,
  128. utilsdirectory,asfound)+target_asm.asmbin+source_os.exeext;
  129. end
  130. else
  131. LastASBin:=FindExe(target_asm.asmbin,asfound);
  132. if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
  133. begin
  134. Message1(exec_w_assembler_not_found,LastASBin);
  135. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  136. end;
  137. if asfound then
  138. Message1(exec_t_using_assembler,LastASBin);
  139. end;
  140. FindAssembler:=LastASBin;
  141. end;
  142. Function TAsmList.CallAssembler(const command,para:string):Boolean;
  143. begin
  144. callassembler:=true;
  145. if not(cs_asm_extern in aktglobalswitches) then
  146. begin
  147. swapvectors;
  148. exec(command,para);
  149. swapvectors;
  150. if (doserror<>0) then
  151. begin
  152. Message1(exec_w_cant_call_assembler,tostr(doserror));
  153. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  154. callassembler:=false;
  155. end
  156. else
  157. if (dosexitcode<>0) then
  158. begin
  159. Message1(exec_w_error_while_assembling,tostr(dosexitcode));
  160. callassembler:=false;
  161. end;
  162. end;
  163. if cs_asm_extern in aktglobalswitches then
  164. AsmRes.AddAsmCommand(command,para,name);
  165. end;
  166. procedure TAsmList.RemoveAsm;
  167. var
  168. g : file;
  169. i : word;
  170. begin
  171. if cs_asm_leave in aktglobalswitches then
  172. exit;
  173. if cs_asm_extern in aktglobalswitches then
  174. AsmRes.AddDeleteCommand(AsmFile)
  175. else
  176. begin
  177. assign(g,AsmFile);
  178. {$I-}
  179. erase(g);
  180. {$I+}
  181. i:=ioresult;
  182. end;
  183. end;
  184. Function TAsmList.DoAssemble:boolean;
  185. var
  186. s : string;
  187. begin
  188. DoAssemble:=true;
  189. if DoPipe then
  190. exit;
  191. if (SmartLinkFilesCnt<=1) and not(cs_asm_extern in aktglobalswitches) then
  192. Message1(exec_i_assembling,name);
  193. s:=target_asm.asmcmd;
  194. Replace(s,'$ASM',AsmFile);
  195. Replace(s,'$OBJ',ObjFile);
  196. if CallAssembler(FindAssembler,s) then
  197. RemoveAsm;
  198. end;
  199. procedure TAsmList.NextSmartName;
  200. var
  201. s : string;
  202. begin
  203. inc(SmartLinkFilesCnt);
  204. if SmartLinkFilesCnt>999999 then
  205. Message(assem_f_too_many_asm_files);
  206. if IsEndFile then
  207. begin
  208. s:=current_module^.asmprefix^+'e';
  209. IsEndFile:=false;
  210. end
  211. else
  212. s:=current_module^.asmprefix^;
  213. AsmFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.asmext);
  214. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext);
  215. end;
  216. {*****************************************************************************
  217. TAsmList AsmFile Writing
  218. *****************************************************************************}
  219. Procedure TAsmList.AsmFlush;
  220. begin
  221. if outcnt>0 then
  222. begin
  223. BlockWrite(outfile,outbuf,outcnt);
  224. outcnt:=0;
  225. end;
  226. end;
  227. Procedure TAsmList.AsmClear;
  228. begin
  229. outcnt:=0;
  230. end;
  231. Procedure TAsmList.AsmWrite(const s:string);
  232. begin
  233. if OutCnt+length(s)>=AsmOutSize then
  234. AsmFlush;
  235. Move(s[1],OutBuf[OutCnt],length(s));
  236. inc(OutCnt,length(s));
  237. inc(AsmSize,length(s));
  238. end;
  239. Procedure TAsmList.AsmWriteLn(const s:string);
  240. begin
  241. AsmWrite(s);
  242. AsmLn;
  243. end;
  244. Procedure TAsmList.AsmWritePChar(p:pchar);
  245. var
  246. i,j : longint;
  247. begin
  248. i:=StrLen(p);
  249. j:=i;
  250. while j>0 do
  251. begin
  252. i:=min(j,AsmOutSize);
  253. if OutCnt+i>=AsmOutSize then
  254. AsmFlush;
  255. Move(p[0],OutBuf[OutCnt],i);
  256. inc(OutCnt,i);
  257. inc(AsmSize,i);
  258. dec(j,i);
  259. p:=pchar(@p[i]);
  260. end;
  261. end;
  262. Procedure TAsmList.AsmLn;
  263. begin
  264. if OutCnt>=AsmOutSize-2 then
  265. AsmFlush;
  266. OutBuf[OutCnt]:=target_os.newline[1];
  267. inc(OutCnt);
  268. inc(AsmSize);
  269. if length(target_os.newline)>1 then
  270. begin
  271. OutBuf[OutCnt]:=target_os.newline[2];
  272. inc(OutCnt);
  273. inc(AsmSize);
  274. end;
  275. end;
  276. procedure TAsmList.AsmCreate;
  277. begin
  278. if (cs_smartlink in aktmoduleswitches) then
  279. NextSmartName;
  280. {$ifdef linux}
  281. if DoPipe then
  282. begin
  283. Message1(exec_i_assembling_pipe,asmfile);
  284. POpen(outfile,'as -o '+objfile,'W');
  285. end
  286. else
  287. {$endif}
  288. begin
  289. Assign(outfile,asmfile);
  290. {$I-}
  291. Rewrite(outfile,1);
  292. {$I+}
  293. if ioresult<>0 then
  294. Message1(exec_d_cant_create_asmfile,asmfile);
  295. end;
  296. outcnt:=0;
  297. AsmSize:=0;
  298. AsmStartSize:=0;
  299. end;
  300. procedure TAsmList.AsmClose;
  301. var
  302. f : file;
  303. l : longint;
  304. begin
  305. AsmFlush;
  306. {$ifdef linux}
  307. if DoPipe then
  308. Close(outfile)
  309. else
  310. {$endif}
  311. begin
  312. {Touch Assembler time to ppu time is there is a ppufilename}
  313. if Assigned(current_module^.ppufilename) then
  314. begin
  315. Assign(f,current_module^.ppufilename^);
  316. {$I-}
  317. reset(f,1);
  318. {$I+}
  319. if ioresult=0 then
  320. begin
  321. getftime(f,l);
  322. close(f);
  323. reset(outfile,1);
  324. setftime(outfile,l);
  325. end;
  326. end;
  327. close(outfile);
  328. end;
  329. end;
  330. {Touch Assembler and object time to ppu time is there is a ppufilename}
  331. procedure TAsmList.Synchronize;
  332. var
  333. f : file;
  334. l : longint;
  335. begin
  336. {Touch Assembler time to ppu time is there is a ppufilename}
  337. if Assigned(current_module^.ppufilename) then
  338. begin
  339. Assign(f,current_module^.ppufilename^);
  340. {$I-}
  341. reset(f,1);
  342. {$I+}
  343. if ioresult=0 then
  344. begin
  345. getftime(f,l);
  346. close(f);
  347. assign(f,asmfile);
  348. {$I-}
  349. reset(f,1);
  350. {$I+}
  351. if ioresult=0 then
  352. begin
  353. setftime(f,l);
  354. close(f);
  355. end;
  356. if not(cs_asm_extern in aktglobalswitches) then
  357. begin
  358. assign(f,objfile);
  359. {$I-}
  360. reset(f,1);
  361. {$I+}
  362. if ioresult=0 then
  363. begin
  364. setftime(f,l);
  365. close(f);
  366. end;
  367. end;
  368. end;
  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;
  378. var
  379. i : word;
  380. begin
  381. { load start values }
  382. asmfile:=current_module^.asmfilename^;
  383. objfile:=current_module^.objfilename^;
  384. name:=FixFileName(current_module^.modulename^);
  385. OutCnt:=0;
  386. SmartLinkFilesCnt:=0;
  387. IsEndFile:=false;
  388. { Which path will be used ? }
  389. if (cs_smartlink in aktmoduleswitches) then
  390. begin
  391. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  392. {$I-}
  393. mkdir(path);
  394. {$I+}
  395. i:=ioresult;
  396. path:=FixPath(path,false);
  397. end
  398. else
  399. path:=current_module^.path^;
  400. end;
  401. Destructor TAsmList.Done;
  402. begin
  403. end;
  404. {*****************************************************************************
  405. Generate Assembler Files Main Procedure
  406. *****************************************************************************}
  407. Procedure GenerateAsm;
  408. var
  409. a : PAsmList;
  410. begin
  411. case aktoutputformat of
  412. {$ifdef i386}
  413. {$ifndef NoAg386Att}
  414. as_i386_o,
  415. as_i386_o_aout,
  416. as_i386_asw :
  417. a:=new(pi386attasmlist,Init);
  418. {$endif NoAg386Att}
  419. {$ifndef NoAg386Nsm}
  420. as_i386_nasmcoff,
  421. as_i386_nasmelf,
  422. as_i386_nasmobj :
  423. a:=new(pi386nasmasmlist,Init);
  424. {$endif NoAg386Nsm}
  425. {$ifndef NoAg386Int}
  426. as_i386_tasm :
  427. a:=new(pi386intasmlist,Init);
  428. {$endif NoAg386Int}
  429. {$endif}
  430. {$ifdef m68k}
  431. {$ifndef NoAg68kGas}
  432. as_m68k_o,
  433. as_m68k_gas :
  434. a:=new(pm68kgasasmlist,Init);
  435. {$endif NoAg86KGas}
  436. {$ifndef NoAg68kMot}
  437. as_m68k_mot :
  438. a:=new(pm68kmotasmlist,Init);
  439. {$endif NoAg86kMot}
  440. {$ifndef NoAg68kMit}
  441. as_m68k_mit :
  442. a:=new(pm68kmitasmlist,Init);
  443. {$endif NoAg86KMot}
  444. {$ifndef NoAg68kMpw}
  445. as_m68k_mpw :
  446. a:=new(pm68kmpwasmlist,Init);
  447. {$endif NoAg68kMpw}
  448. {$endif}
  449. else
  450. Message(assem_f_assembler_output_not_supported);
  451. end;
  452. a^.AsmCreate;
  453. a^.WriteAsmList;
  454. a^.AsmClose;
  455. a^.DoAssemble;
  456. a^.synchronize;
  457. dispose(a,Done);
  458. end;
  459. Procedure OnlyAsm;
  460. var
  461. a : PAsmList;
  462. begin
  463. a:=new(pasmlist,Init);
  464. a^.DoAssemble;
  465. dispose(a,Done);
  466. end;
  467. end.
  468. {
  469. $Log$
  470. Revision 1.31 1998-10-26 22:23:28 peter
  471. + fixpath() has an extra option to allow a ./ as path
  472. Revision 1.30 1998/10/16 13:37:14 florian
  473. + switch -FD added to specify the path for utilities
  474. Revision 1.29 1998/10/15 16:19:42 peter
  475. * fixed asmsynchronize
  476. Revision 1.28 1998/10/14 15:56:43 pierre
  477. * all references to comp suppressed for m68k
  478. Revision 1.27 1998/10/13 16:50:01 pierre
  479. * undid some changes of Peter that made the compiler wrong
  480. for m68k (I had to reinsert some ifdefs)
  481. * removed several memory leaks under m68k
  482. * removed the meory leaks for assembler readers
  483. * cross compiling shoud work again better
  484. ( crosscompiling sysamiga works
  485. but as68k still complain about some code !)
  486. Revision 1.26 1998/10/13 13:10:11 peter
  487. * new style for m68k/i386 infos and enums
  488. Revision 1.25 1998/10/13 08:19:24 pierre
  489. + source_os is now set correctly for cross-processor compilers
  490. (tos contains all target_infos and
  491. we use CPU86 and CPU68 conditionnals to
  492. get the source operating system
  493. this only works if you do not undefine
  494. the source target !!)
  495. * several cg68k memory leaks fixed
  496. + started to change the code so that it should be possible to have
  497. a complete compiler (both for m68k and i386 !!)
  498. Revision 1.24 1998/10/08 23:28:50 peter
  499. * -vu shows unit info, -vt shows tried/used files
  500. Revision 1.23 1998/10/07 04:27:37 carl
  501. + MPW support
  502. Revision 1.22 1998/09/16 16:41:39 peter
  503. * merged fixes
  504. Revision 1.21.2.1 1998/09/16 16:11:38 peter
  505. * missing isendfile reset in .init
  506. Revision 1.21 1998/09/07 18:33:32 peter
  507. + smartlinking for win95 imports
  508. Revision 1.20 1998/09/04 17:34:20 pierre
  509. * bug with datalabel corrected
  510. + assembler errors better commented
  511. * one nested record crash removed
  512. Revision 1.19 1998/08/26 10:06:34 peter
  513. * reduce amount of asmfiles generated
  514. * no stabs are written in writefilelineinfo when debuginfo is off
  515. Revision 1.18 1998/08/21 14:08:39 pierre
  516. + TEST_FUNCRET now default (old code removed)
  517. works also for m68k (at least compiles)
  518. Revision 1.17 1998/08/17 09:17:43 peter
  519. * static/shared linking updates
  520. Revision 1.16 1998/08/14 21:56:30 peter
  521. * setting the outputfile using -o works now to create static libs
  522. Revision 1.15 1998/08/14 18:16:09 peter
  523. * return after a failed call will now add it to ppas
  524. Revision 1.14 1998/08/10 14:49:41 peter
  525. + localswitches, moduleswitches, globalswitches splitting
  526. Revision 1.13 1998/07/14 21:46:40 peter
  527. * updated messages file
  528. Revision 1.12 1998/07/08 14:58:34 daniel
  529. * First check if call to assembler is succesfull, then check it's exit code.
  530. This is more logical than first checking the exit code. For some mysterious
  531. reason this did not give problems on DOS & Linux. On OS/2 it did.
  532. Revision 1.11 1998/06/08 22:59:43 peter
  533. * smartlinking works for win32
  534. * some defines to exclude some compiler parts
  535. Revision 1.10 1998/06/04 23:51:33 peter
  536. * m68k compiles
  537. + .def file creation moved to gendef.pas so it could also be used
  538. for win32
  539. Revision 1.9 1998/05/23 01:21:01 peter
  540. + aktasmmode, aktoptprocessor, aktoutputformat
  541. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  542. + $LIBNAME to set the library name where the unit will be put in
  543. * splitted cgi386 a bit (codeseg to large for bp7)
  544. * nasm, tasm works again. nasm moved to ag386nsm.pas
  545. Revision 1.8 1998/05/11 13:07:53 peter
  546. + $ifdef NEWPPU for the new ppuformat
  547. + $define GDB not longer required
  548. * removed all warnings and stripped some log comments
  549. * no findfirst/findnext anymore to remove smartlink *.o files
  550. Revision 1.7 1998/05/07 00:17:00 peter
  551. * smartlinking for sets
  552. + consts labels are now concated/generated in hcodegen
  553. * moved some cpu code to cga and some none cpu depended code from cga
  554. to tree and hcodegen and cleanup of hcodegen
  555. * assembling .. output reduced for smartlinking ;)
  556. Revision 1.6 1998/05/04 17:54:24 peter
  557. + smartlinking works (only case jumptable left todo)
  558. * redesign of systems.pas to support assemblers and linkers
  559. + Unitname is now also in the PPU-file, increased version to 14
  560. Revision 1.5 1998/04/29 10:33:44 pierre
  561. + added some code for ansistring (not complete nor working yet)
  562. * corrected operator overloading
  563. * corrected nasm output
  564. + started inline procedures
  565. + added starstarn : use ** for exponentiation (^ gave problems)
  566. + started UseTokenInfo cond to get accurate positions
  567. Revision 1.4 1998/04/27 23:10:27 peter
  568. + new scanner
  569. * $makelib -> if smartlink
  570. * small filename fixes pmodule.setfilename
  571. * moved import from files.pas -> import.pas
  572. Revision 1.3 1998/04/10 14:41:43 peter
  573. * removed some Hints
  574. * small speed optimization for AsmLn
  575. Revision 1.2 1998/04/08 11:34:18 peter
  576. * nasm works (linux only tested)
  577. }