assemble.pas 17 KB

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