assemble.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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=10000;
  26. {$endif}
  27. type
  28. PAsmList=^TAsmList;
  29. TAsmList=object
  30. {filenames}
  31. path : dirstr;
  32. name : namestr;
  33. asmfile,
  34. objfile,
  35. srcfile,
  36. as_bin : string;
  37. {outfile}
  38. outcnt : longint;
  39. outbuf : array[0..AsmOutSize-1] of char;
  40. outfile : file;
  41. Constructor Init(const fn:string);
  42. Destructor Done;
  43. Function FindAssembler:string;
  44. Function CallAssembler(const command,para:string):Boolean;
  45. Function DoAssemble:boolean;
  46. Procedure RemoveAsm;
  47. procedure NextSmartName;
  48. Procedure AsmFlush;
  49. Procedure AsmWrite(const s:string);
  50. Procedure AsmWritePChar(p:pchar);
  51. Procedure AsmWriteLn(const s:string);
  52. Procedure AsmLn;
  53. procedure AsmCreate;
  54. procedure AsmClose;
  55. procedure WriteTree(p:paasmoutput);virtual;
  56. procedure WriteAsmList;virtual;
  57. end;
  58. Procedure GenerateAsm(const fn:string);
  59. Procedure OnlyAsm(const fn:string);
  60. var
  61. SmartLinkFilesCnt : longint;
  62. Function SmartLinkPath(const s:string):string;
  63. Implementation
  64. uses
  65. script,files,systems,verbose
  66. {$ifdef linux}
  67. ,linux
  68. {$endif}
  69. ,strings
  70. {$ifdef i386}
  71. ,ag386att,ag386int,ag386nsm
  72. {$endif}
  73. {$ifdef m68k}
  74. ,ag68kmot,ag68kgas,ag68kmit
  75. {$endif}
  76. ;
  77. {*****************************************************************************
  78. SmartLink Helpers
  79. *****************************************************************************}
  80. Function SmartLinkPath(const s:string):string;
  81. var
  82. p : dirstr;
  83. n : namestr;
  84. e : extstr;
  85. begin
  86. FSplit(s,p,n,e);
  87. SmartLinkPath:=FixFileName(n+target_info.smartext);
  88. end;
  89. {*****************************************************************************
  90. TAsmList
  91. *****************************************************************************}
  92. Function DoPipe:boolean;
  93. begin
  94. DoPipe:=use_pipe and (not WriteAsmFile) and (aktoutputformat=as_o);
  95. end;
  96. const
  97. lastas : byte=255;
  98. var
  99. LastASBin : string;
  100. Function TAsmList.FindAssembler:string;
  101. var
  102. asfound : boolean;
  103. begin
  104. if lastas<>ord(target_asm.id) then
  105. begin
  106. lastas:=ord(target_asm.id);
  107. LastASBin:=FindExe(target_asm.asmbin,asfound);
  108. if (not asfound) and (not externasm) then
  109. begin
  110. Message1(exec_w_assembler_not_found,LastASBin);
  111. externasm:=true;
  112. end;
  113. if asfound then
  114. Message1(exec_u_using_assembler,LastASBin);
  115. end;
  116. FindAssembler:=LastASBin;
  117. end;
  118. Function TAsmList.CallAssembler(const command,para:string):Boolean;
  119. begin
  120. if not externasm then
  121. begin
  122. swapvectors;
  123. exec(command,para);
  124. swapvectors;
  125. if (dosexitcode<>0) then
  126. begin
  127. Message(exec_w_error_while_assembling);
  128. callassembler:=false;
  129. exit;
  130. end
  131. else
  132. if (doserror<>0) then
  133. begin
  134. Message(exec_w_cant_call_assembler);
  135. externasm:=true;
  136. end;
  137. end;
  138. if externasm then
  139. AsmRes.AddAsmCommand(command,para,asmfile);
  140. callassembler:=true;
  141. end;
  142. procedure TAsmList.RemoveAsm;
  143. var
  144. g : file;
  145. i : word;
  146. begin
  147. if writeasmfile then
  148. exit;
  149. if ExternAsm then
  150. AsmRes.AddDeleteCommand(asmfile)
  151. else
  152. begin
  153. assign(g,asmfile);
  154. {$I-}
  155. erase(g);
  156. {$I+}
  157. i:=ioresult;
  158. end;
  159. end;
  160. Function TAsmList.DoAssemble:boolean;
  161. var
  162. s : string;
  163. begin
  164. DoAssemble:=true;
  165. if DoPipe then
  166. exit;
  167. if (SmartLinkFilesCnt<=1) and (not externasm) then
  168. Message1(exec_i_assembling,name);
  169. s:=target_asm.asmcmd;
  170. Replace(s,'$ASM',AsmFile);
  171. Replace(s,'$OBJ',ObjFile);
  172. if CallAssembler(FindAssembler,s) then
  173. RemoveAsm;
  174. end;
  175. procedure TAsmList.NextSmartName;
  176. begin
  177. inc(SmartLinkFilesCnt);
  178. if SmartLinkFilesCnt>999999 then
  179. Comment(V_Fatal,'Too many assembler files');
  180. AsmFile:=Path+FixFileName('as'+tostr(SmartLinkFilesCnt)+target_info.asmext);
  181. ObjFile:=Path+FixFileName('as'+tostr(SmartLinkFilesCnt)+target_info.objext);
  182. end;
  183. {*****************************************************************************
  184. TAsmList AsmFile Writing
  185. *****************************************************************************}
  186. Procedure TAsmList.AsmFlush;
  187. begin
  188. if outcnt>0 then
  189. begin
  190. BlockWrite(outfile,outbuf,outcnt);
  191. outcnt:=0;
  192. end;
  193. end;
  194. Procedure TAsmList.AsmWrite(const s:string);
  195. begin
  196. if OutCnt+length(s)>=AsmOutSize then
  197. AsmFlush;
  198. Move(s[1],OutBuf[OutCnt],length(s));
  199. inc(OutCnt,length(s));
  200. end;
  201. Procedure TAsmList.AsmWriteLn(const s:string);
  202. begin
  203. AsmWrite(s);
  204. AsmLn;
  205. end;
  206. Procedure TAsmList.AsmWritePChar(p:pchar);
  207. var
  208. i,j : longint;
  209. begin
  210. i:=StrLen(p);
  211. j:=i;
  212. while j>0 do
  213. begin
  214. i:=min(j,AsmOutSize);
  215. if OutCnt+i>=AsmOutSize then
  216. AsmFlush;
  217. Move(p[0],OutBuf[OutCnt],i);
  218. inc(OutCnt,i);
  219. dec(j,i);
  220. p:=pchar(@p[i]);
  221. end;
  222. end;
  223. Procedure TAsmList.AsmLn;
  224. begin
  225. if OutCnt>=AsmOutSize-2 then
  226. AsmFlush;
  227. OutBuf[OutCnt]:=target_os.newline[1];
  228. inc(OutCnt);
  229. if length(target_os.newline)>1 then
  230. begin
  231. OutBuf[OutCnt]:=target_os.newline[2];
  232. inc(OutCnt);
  233. end;
  234. end;
  235. procedure TAsmList.AsmCreate;
  236. begin
  237. if (cs_smartlink in aktswitches) then
  238. NextSmartName;
  239. {$ifdef linux}
  240. if DoPipe then
  241. begin
  242. Message1(exec_i_assembling_pipe,asmfile);
  243. POpen(outfile,'as -o '+objfile,'W');
  244. end
  245. else
  246. {$endif}
  247. begin
  248. Assign(outfile,asmfile);
  249. {$I-}
  250. Rewrite(outfile,1);
  251. {$I+}
  252. if ioresult<>0 then
  253. Message1(exec_d_cant_create_asmfile,asmfile);
  254. end;
  255. outcnt:=0;
  256. end;
  257. procedure TAsmList.AsmClose;
  258. var
  259. f : file;
  260. l : longint;
  261. begin
  262. AsmFlush;
  263. {$ifdef linux}
  264. if DoPipe then
  265. Close(outfile)
  266. else
  267. {$endif}
  268. begin
  269. {Touch Assembler time to ppu time is there is a ppufilename}
  270. if Assigned(current_module^.ppufilename) then
  271. begin
  272. Assign(f,current_module^.ppufilename^);
  273. {$I-}
  274. reset(f,1);
  275. {$I+}
  276. if ioresult=0 then
  277. begin
  278. getftime(f,l);
  279. close(f);
  280. reset(outfile,1);
  281. setftime(outfile,l);
  282. end;
  283. end;
  284. close(outfile);
  285. end;
  286. end;
  287. procedure TAsmList.WriteTree(p:paasmoutput);
  288. begin
  289. end;
  290. procedure TAsmList.WriteAsmList;
  291. begin
  292. end;
  293. Constructor TAsmList.Init(const fn:string);
  294. var
  295. ext : extstr;
  296. i : word;
  297. begin
  298. {Create filenames for easier access}
  299. fsplit(fn,path,name,ext);
  300. srcfile:=fn;
  301. asmfile:=path+name+target_info.asmext;
  302. objfile:=path+name+target_info.objext;
  303. OutCnt:=0;
  304. {Smartlinking}
  305. SmartLinkFilesCnt:=0;
  306. if (cs_smartlink in aktswitches) then
  307. begin
  308. path:=SmartLinkPath(name);
  309. {$I-}
  310. mkdir(path);
  311. {$I+}
  312. i:=ioresult;
  313. end;
  314. path:=FixPath(path);
  315. end;
  316. Destructor TAsmList.Done;
  317. begin
  318. end;
  319. {*****************************************************************************
  320. Generate Assembler Files Main Procedure
  321. *****************************************************************************}
  322. Procedure GenerateAsm(const fn:string);
  323. var
  324. a : PAsmList;
  325. begin
  326. case aktoutputformat of
  327. {$ifdef i386}
  328. as_o : a:=new(pi386attasmlist,Init(fn));
  329. as_nasmcoff,
  330. as_nasmelf,
  331. as_nasmobj : a:=new(pi386nasmasmlist,Init(fn));
  332. as_tasm : a:=new(pi386intasmlist,Init(fn));
  333. {$endif}
  334. {$ifdef m68k}
  335. as_o,
  336. as_gas : a:=new(pm68kgasasmlist,Init(fn));
  337. as_mot : a:=new(pm68kmotasmlist,Init(fn));
  338. as_mit : a:=new(pm68kmitasmlist,Init(fn));
  339. {$endif}
  340. else
  341. internalerror(30000);
  342. end;
  343. a^.AsmCreate;
  344. a^.WriteAsmList;
  345. a^.AsmClose;
  346. a^.DoAssemble;
  347. dispose(a,Done);
  348. end;
  349. Procedure OnlyAsm(const fn:string);
  350. var
  351. a : PAsmList;
  352. begin
  353. a:=new(pasmlist,Init(fn));
  354. a^.DoAssemble;
  355. dispose(a,Done);
  356. end;
  357. end.
  358. {
  359. $Log$
  360. Revision 1.9 1998-05-23 01:21:01 peter
  361. + aktasmmode, aktoptprocessor, aktoutputformat
  362. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  363. + $LIBNAME to set the library name where the unit will be put in
  364. * splitted cgi386 a bit (codeseg to large for bp7)
  365. * nasm, tasm works again. nasm moved to ag386nsm.pas
  366. Revision 1.8 1998/05/11 13:07:53 peter
  367. + $ifdef NEWPPU for the new ppuformat
  368. + $define GDB not longer required
  369. * removed all warnings and stripped some log comments
  370. * no findfirst/findnext anymore to remove smartlink *.o files
  371. Revision 1.7 1998/05/07 00:17:00 peter
  372. * smartlinking for sets
  373. + consts labels are now concated/generated in hcodegen
  374. * moved some cpu code to cga and some none cpu depended code from cga
  375. to tree and hcodegen and cleanup of hcodegen
  376. * assembling .. output reduced for smartlinking ;)
  377. Revision 1.6 1998/05/04 17:54:24 peter
  378. + smartlinking works (only case jumptable left todo)
  379. * redesign of systems.pas to support assemblers and linkers
  380. + Unitname is now also in the PPU-file, increased version to 14
  381. Revision 1.5 1998/04/29 10:33:44 pierre
  382. + added some code for ansistring (not complete nor working yet)
  383. * corrected operator overloading
  384. * corrected nasm output
  385. + started inline procedures
  386. + added starstarn : use ** for exponentiation (^ gave problems)
  387. + started UseTokenInfo cond to get accurate positions
  388. Revision 1.4 1998/04/27 23:10:27 peter
  389. + new scanner
  390. * $makelib -> if smartlink
  391. * small filename fixes pmodule.setfilename
  392. * moved import from files.pas -> import.pas
  393. Revision 1.3 1998/04/10 14:41:43 peter
  394. * removed some Hints
  395. * small speed optimization for AsmLn
  396. Revision 1.2 1998/04/08 11:34:18 peter
  397. * nasm works (linux only tested)
  398. }