assemble.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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_as)
  114. {$endif i386}
  115. {$ifdef m68k}
  116. and (aktoutputformat=as_m68k_as);
  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. begin
  344. {Touch Assembler time to ppu time is there is a ppufilename}
  345. if Assigned(current_module^.ppufilename) then
  346. begin
  347. SynchronizeFileTime(current_module^.ppufilename^,asmfile);
  348. if not(cs_asm_extern in aktglobalswitches) then
  349. SynchronizeFileTime(current_module^.ppufilename^,objfile);
  350. end;
  351. end;
  352. procedure TAsmList.WriteTree(p:paasmoutput);
  353. begin
  354. end;
  355. procedure TAsmList.WriteAsmList;
  356. begin
  357. end;
  358. Constructor TAsmList.Init;
  359. var
  360. i : word;
  361. begin
  362. { load start values }
  363. asmfile:=current_module^.asmfilename^;
  364. objfile:=current_module^.objfilename^;
  365. name:=FixFileName(current_module^.modulename^);
  366. OutCnt:=0;
  367. SmartLinkFilesCnt:=0;
  368. IsEndFile:=false;
  369. { Which path will be used ? }
  370. if (cs_smartlink in aktmoduleswitches) then
  371. begin
  372. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  373. {$I-}
  374. mkdir(path);
  375. {$I+}
  376. i:=ioresult;
  377. path:=FixPath(path,false);
  378. end
  379. else
  380. path:=current_module^.path^;
  381. end;
  382. Destructor TAsmList.Done;
  383. begin
  384. end;
  385. {*****************************************************************************
  386. Generate Assembler Files Main Procedure
  387. *****************************************************************************}
  388. Procedure GenerateAsm;
  389. var
  390. a : PAsmList;
  391. {$ifdef i386}
  392. {$ifdef Ag386Bin}
  393. b : Pi386binasmlist;
  394. {$endif}
  395. {$endif}
  396. begin
  397. case aktoutputformat of
  398. {$ifdef i386}
  399. {$ifdef Ag386Bin}
  400. as_i386_dbg,
  401. as_i386_coff,
  402. as_i386_pecoff :
  403. begin
  404. case aktoutputformat of
  405. as_i386_dbg :
  406. b:=new(pi386binasmlist,Init(og_dbg));
  407. as_i386_coff :
  408. b:=new(pi386binasmlist,Init(og_coff));
  409. as_i386_pecoff :
  410. b:=new(pi386binasmlist,Init(og_pecoff));
  411. end;
  412. b^.WriteBin;
  413. dispose(b,done);
  414. if assigned(current_module^.ppufilename) then
  415. SynchronizeFileTime(current_module^.ppufilename^,current_module^.objfilename^);
  416. exit;
  417. end;
  418. {$endif Ag386Bin}
  419. {$ifndef NoAg386Att}
  420. as_i386_as,
  421. as_i386_as_aout,
  422. as_i386_asw :
  423. a:=new(pi386attasmlist,Init);
  424. {$endif NoAg386Att}
  425. {$ifndef NoAg386Nsm}
  426. as_i386_nasmcoff,
  427. as_i386_nasmelf,
  428. as_i386_nasmobj :
  429. a:=new(pi386nasmasmlist,Init);
  430. {$endif NoAg386Nsm}
  431. {$ifndef NoAg386Int}
  432. as_i386_tasm :
  433. a:=new(pi386intasmlist,Init);
  434. {$endif NoAg386Int}
  435. {$endif}
  436. {$ifdef m68k}
  437. {$ifndef NoAg68kGas}
  438. as_m68k_as,
  439. as_m68k_gas :
  440. a:=new(pm68kgasasmlist,Init);
  441. {$endif NoAg86KGas}
  442. {$ifndef NoAg68kMot}
  443. as_m68k_mot :
  444. a:=new(pm68kmotasmlist,Init);
  445. {$endif NoAg86kMot}
  446. {$ifndef NoAg68kMit}
  447. as_m68k_mit :
  448. a:=new(pm68kmitasmlist,Init);
  449. {$endif NoAg86KMot}
  450. {$ifndef NoAg68kMpw}
  451. as_m68k_mpw :
  452. a:=new(pm68kmpwasmlist,Init);
  453. {$endif NoAg68kMpw}
  454. {$endif}
  455. else
  456. Message(assem_f_assembler_output_not_supported);
  457. end;
  458. a^.AsmCreate;
  459. a^.WriteAsmList;
  460. a^.AsmClose;
  461. a^.DoAssemble;
  462. a^.synchronize;
  463. dispose(a,Done);
  464. end;
  465. Procedure OnlyAsm;
  466. var
  467. a : PAsmList;
  468. begin
  469. a:=new(pasmlist,Init);
  470. a^.DoAssemble;
  471. dispose(a,Done);
  472. end;
  473. end.
  474. {
  475. $Log$
  476. Revision 1.39 1999-03-01 15:43:48 peter
  477. * synchronize also the objfile for ag386bin
  478. Revision 1.38 1999/02/26 00:48:15 peter
  479. * assembler writers fixed for ag386bin
  480. Revision 1.37 1999/02/24 00:59:11 peter
  481. * small updates for ag386bin
  482. Revision 1.36 1999/02/22 02:15:01 peter
  483. * updates for ag386bin
  484. Revision 1.35 1999/02/17 10:16:26 peter
  485. * small fixes for the binary writer
  486. Revision 1.34 1999/01/10 15:37:52 peter
  487. * moved some tables from ra386*.pas -> i386.pas
  488. + start of coff writer
  489. * renamed asmutils unit to rautils
  490. Revision 1.33 1998/12/11 00:02:45 peter
  491. + globtype,tokens,version unit splitted from globals
  492. Revision 1.32 1998/11/06 09:46:46 pierre
  493. * assemble failure increments status errorcount again !!
  494. Revision 1.31 1998/10/26 22:23:28 peter
  495. + fixpath() has an extra option to allow a ./ as path
  496. Revision 1.30 1998/10/16 13:37:14 florian
  497. + switch -FD added to specify the path for utilities
  498. Revision 1.29 1998/10/15 16:19:42 peter
  499. * fixed asmsynchronize
  500. Revision 1.28 1998/10/14 15:56:43 pierre
  501. * all references to comp suppressed for m68k
  502. Revision 1.27 1998/10/13 16:50:01 pierre
  503. * undid some changes of Peter that made the compiler wrong
  504. for m68k (I had to reinsert some ifdefs)
  505. * removed several memory leaks under m68k
  506. * removed the meory leaks for assembler readers
  507. * cross compiling shoud work again better
  508. ( crosscompiling sysamiga works
  509. but as68k still complain about some code !)
  510. Revision 1.26 1998/10/13 13:10:11 peter
  511. * new style for m68k/i386 infos and enums
  512. Revision 1.25 1998/10/13 08:19:24 pierre
  513. + source_os is now set correctly for cross-processor compilers
  514. (tos contains all target_infos and
  515. we use CPU86 and CPU68 conditionnals to
  516. get the source operating system
  517. this only works if you do not undefine
  518. the source target !!)
  519. * several cg68k memory leaks fixed
  520. + started to change the code so that it should be possible to have
  521. a complete compiler (both for m68k and i386 !!)
  522. Revision 1.24 1998/10/08 23:28:50 peter
  523. * -vu shows unit info, -vt shows tried/used files
  524. Revision 1.23 1998/10/07 04:27:37 carl
  525. + MPW support
  526. Revision 1.22 1998/09/16 16:41:39 peter
  527. * merged fixes
  528. Revision 1.21.2.1 1998/09/16 16:11:38 peter
  529. * missing isendfile reset in .init
  530. Revision 1.21 1998/09/07 18:33:32 peter
  531. + smartlinking for win95 imports
  532. Revision 1.20 1998/09/04 17:34:20 pierre
  533. * bug with datalabel corrected
  534. + assembler errors better commented
  535. * one nested record crash removed
  536. Revision 1.19 1998/08/26 10:06:34 peter
  537. * reduce amount of asmfiles generated
  538. * no stabs are written in writefilelineinfo when debuginfo is off
  539. Revision 1.18 1998/08/21 14:08:39 pierre
  540. + TEST_FUNCRET now default (old code removed)
  541. works also for m68k (at least compiles)
  542. Revision 1.17 1998/08/17 09:17:43 peter
  543. * static/shared linking updates
  544. Revision 1.16 1998/08/14 21:56:30 peter
  545. * setting the outputfile using -o works now to create static libs
  546. Revision 1.15 1998/08/14 18:16:09 peter
  547. * return after a failed call will now add it to ppas
  548. Revision 1.14 1998/08/10 14:49:41 peter
  549. + localswitches, moduleswitches, globalswitches splitting
  550. Revision 1.13 1998/07/14 21:46:40 peter
  551. * updated messages file
  552. Revision 1.12 1998/07/08 14:58:34 daniel
  553. * First check if call to assembler is succesfull, then check it's exit code.
  554. This is more logical than first checking the exit code. For some mysterious
  555. reason this did not give problems on DOS & Linux. On OS/2 it did.
  556. Revision 1.11 1998/06/08 22:59:43 peter
  557. * smartlinking works for win32
  558. * some defines to exclude some compiler parts
  559. Revision 1.10 1998/06/04 23:51:33 peter
  560. * m68k compiles
  561. + .def file creation moved to gendef.pas so it could also be used
  562. for win32
  563. Revision 1.9 1998/05/23 01:21:01 peter
  564. + aktasmmode, aktoptprocessor, aktoutputformat
  565. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  566. + $LIBNAME to set the library name where the unit will be put in
  567. * splitted cgi386 a bit (codeseg to large for bp7)
  568. * nasm, tasm works again. nasm moved to ag386nsm.pas
  569. Revision 1.8 1998/05/11 13:07:53 peter
  570. + $ifdef NEWPPU for the new ppuformat
  571. + $define GDB not longer required
  572. * removed all warnings and stripped some log comments
  573. * no findfirst/findnext anymore to remove smartlink *.o files
  574. Revision 1.7 1998/05/07 00:17:00 peter
  575. * smartlinking for sets
  576. + consts labels are now concated/generated in hcodegen
  577. * moved some cpu code to cga and some none cpu depended code from cga
  578. to tree and hcodegen and cleanup of hcodegen
  579. * assembling .. output reduced for smartlinking ;)
  580. Revision 1.6 1998/05/04 17:54:24 peter
  581. + smartlinking works (only case jumptable left todo)
  582. * redesign of systems.pas to support assemblers and linkers
  583. + Unitname is now also in the PPU-file, increased version to 14
  584. Revision 1.5 1998/04/29 10:33:44 pierre
  585. + added some code for ansistring (not complete nor working yet)
  586. * corrected operator overloading
  587. * corrected nasm output
  588. + started inline procedures
  589. + added starstarn : use ** for exponentiation (^ gave problems)
  590. + started UseTokenInfo cond to get accurate positions
  591. Revision 1.4 1998/04/27 23:10:27 peter
  592. + new scanner
  593. * $makelib -> if smartlink
  594. * small filename fixes pmodule.setfilename
  595. * moved import from files.pas -> import.pas
  596. Revision 1.3 1998/04/10 14:41:43 peter
  597. * removed some Hints
  598. * small speed optimization for AsmLn
  599. Revision 1.2 1998/04/08 11:34:18 peter
  600. * nasm works (linux only tested)
  601. }