compiler.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit is the interface of the compiler which can be used by
  5. external programs to link in the compiler
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************}
  18. {
  19. possible compiler switches:
  20. -----------------------------------------------------------------
  21. TP to compile the compiler with Turbo or Borland Pascal
  22. I386 generate a compiler for the Intel i386+
  23. M68K generate a compiler for the M68000
  24. GDB support of the GNU Debugger
  25. EXTDEBUG some extra debug code is executed
  26. SUPPORT_MMX only i386: releases the compiler switch
  27. MMX which allows the compiler to generate
  28. MMX instructions
  29. EXTERN_MSG Don't compile the msgfiles in the compiler, always
  30. use external messagefiles
  31. NOAG386INT no Intel Assembler output
  32. NOAG386NSM no NASM output
  33. -----------------------------------------------------------------
  34. }
  35. {$ifdef FPC}
  36. { One of Alpha, I386 or M68K must be defined }
  37. {$UNDEF CPUOK}
  38. {$ifdef I386}
  39. {$define CPUOK}
  40. {$endif}
  41. {$ifdef M68K}
  42. {$ifndef CPUOK}
  43. {$DEFINE CPUOK}
  44. {$else}
  45. {$fatal cannot define two CPU switches}
  46. {$endif}
  47. {$endif}
  48. {$ifdef alpha}
  49. {$ifndef CPUOK}
  50. {$DEFINE CPUOK}
  51. {$else}
  52. {$fatal cannot define two CPU switches}
  53. {$endif}
  54. {$endif}
  55. {$ifdef powerpc}
  56. {$ifndef CPUOK}
  57. {$DEFINE CPUOK}
  58. {$else}
  59. {$fatal cannot define two CPU switches}
  60. {$endif}
  61. {$endif}
  62. {$ifndef CPUOK}
  63. {$fatal One of the switches I386, Alpha, PowerPC or M68K must be defined}
  64. {$endif}
  65. {$ifdef support_mmx}
  66. {$ifndef i386}
  67. {$fatal I386 switch must be on for MMX support}
  68. {$endif i386}
  69. {$endif support_mmx}
  70. {$endif}
  71. unit compiler;
  72. interface
  73. { Use exception catching so the compiler goes futher after a Stop }
  74. {$ifndef NOUSEEXCEPT}
  75. {$ifdef i386}
  76. {$define USEEXCEPT}
  77. {$endif}
  78. {$ifdef TP}
  79. {$ifdef DPMI}
  80. {$undef USEEXCEPT}
  81. {$endif}
  82. {$endif}
  83. {$endif ndef NOUSEEXCEPT}
  84. uses
  85. {$ifdef fpc}
  86. {$ifdef GO32V2}
  87. emu387,
  88. { dpmiexcp, }
  89. {$endif GO32V2}
  90. {$ifdef LINUX}
  91. catch,
  92. {$endif LINUX}
  93. {$endif}
  94. {$ifdef USEEXCEPT}
  95. tpexcept,
  96. {$endif USEEXCEPT}
  97. {$ifdef BrowserLog}
  98. browlog,
  99. {$endif BrowserLog}
  100. {$ifdef BrowserCol}
  101. browcol,
  102. {$endif BrowserCol}
  103. {$ifdef Delphi}
  104. dmisc,
  105. {$else Delphi}
  106. dos,
  107. {$endif Delphi}
  108. verbose,comphook,systems,
  109. cobjects,globals,options,parser,symtable,link,import,export,tokens;
  110. function Compile(const cmd:string):longint;
  111. Const
  112. { do we need to link }
  113. IsExe : boolean = false;
  114. implementation
  115. uses
  116. cpubase;
  117. var
  118. CompilerInitedAfterArgs,
  119. CompilerInited : boolean;
  120. olddo_stop : tstopprocedure;
  121. {$ifdef USEEXCEPT}
  122. procedure RecoverStop;{$ifndef FPC}far;{$endif}
  123. begin
  124. if recoverpospointer<>nil then
  125. LongJmp(recoverpospointer^,1)
  126. else
  127. Do_Halt(1);
  128. end;
  129. {$endif USEEXCEPT}
  130. {$ifdef EXTDEBUG}
  131. {$ifdef FPC}
  132. Var
  133. LostMemory : longint;
  134. Procedure CheckMemory(LostMemory : longint);
  135. begin
  136. if LostMemory<>0 then
  137. begin
  138. Writeln('Memory Lost = '+tostr(LostMemory));
  139. {$ifdef DEBUG}
  140. def_gdb_stop(V_Warning);
  141. {$endif DEBUG}
  142. end;
  143. end;
  144. {$endif FPC}
  145. {$endif EXTDEBUG}
  146. {****************************************************************************
  147. Compiler
  148. ****************************************************************************}
  149. procedure DoneCompiler;
  150. begin
  151. if not CompilerInited then
  152. exit;
  153. { Free compiler if args are read }
  154. {$ifdef BrowserLog}
  155. DoneBrowserLog;
  156. {$endif BrowserLog}
  157. {$ifdef BrowserCol}
  158. DoneBrowserCol;
  159. {$endif BrowserCol}
  160. if CompilerInitedAfterArgs then
  161. begin
  162. CompilerInitedAfterArgs:=false;
  163. doneparser;
  164. DoneImport;
  165. DoneExport;
  166. DoneLinker;
  167. DoneCpu;
  168. end;
  169. { Free memory for the others }
  170. CompilerInited:=false;
  171. DoneSymtable;
  172. DoneGlobals;
  173. donetokens;
  174. {$ifdef USEEXCEPT}
  175. recoverpospointer:=nil;
  176. longjump_used:=false;
  177. {$endif USEEXCEPT}
  178. end;
  179. procedure InitCompiler(const cmd:string);
  180. begin
  181. if CompilerInited then
  182. DoneCompiler;
  183. { inits which need to be done before the arguments are parsed }
  184. InitSystems;
  185. InitVerbose;
  186. {$ifdef BrowserLog}
  187. InitBrowserLog;
  188. {$endif BrowserLog}
  189. {$ifdef BrowserCol}
  190. InitBrowserCol;
  191. {$endif BrowserCol}
  192. InitGlobals;
  193. inittokens;
  194. InitSymtable;
  195. CompilerInited:=true;
  196. { read the arguments }
  197. read_arguments(cmd);
  198. { inits which depend on arguments }
  199. initparser;
  200. InitImport;
  201. InitExport;
  202. InitLinker;
  203. InitCpu;
  204. CompilerInitedAfterArgs:=true;
  205. end;
  206. procedure minimal_stop;{$ifndef fpc}far;{$endif}
  207. begin
  208. DoneCompiler;
  209. olddo_stop;
  210. end;
  211. function Compile(const cmd:string):longint;
  212. {$ifdef fpc}
  213. {$maxfpuregisters 0}
  214. {$endif fpc}
  215. procedure writepathlist(w:tmsgconst;l:TSearchPathList);
  216. var
  217. hp : pstringqueueitem;
  218. begin
  219. hp:=l.first;
  220. while assigned(hp) do
  221. begin
  222. Message1(w,hp^.data^);
  223. hp:=hp^.next;
  224. end;
  225. end;
  226. function getrealtime : real;
  227. var
  228. h,m,s,s100 : word;
  229. begin
  230. gettime(h,m,s,s100);
  231. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  232. end;
  233. var
  234. starttime : real;
  235. {$ifdef USEEXCEPT}
  236. recoverpos : jmp_buf;
  237. {$endif}
  238. begin
  239. olddo_stop:=do_stop;
  240. {$ifdef TP}
  241. do_stop:=minimal_stop;
  242. {$else TP}
  243. do_stop:=@minimal_stop;
  244. {$endif TP}
  245. { Initialize the compiler }
  246. InitCompiler(cmd);
  247. { show some info }
  248. Message1(general_t_compilername,FixFileName(paramstr(0)));
  249. Message1(general_d_sourceos,source_os.name);
  250. Message1(general_i_targetos,target_os.name);
  251. Message1(general_t_exepath,exepath);
  252. WritePathList(general_t_unitpath,unitsearchpath);
  253. WritePathList(general_t_includepath,includesearchpath);
  254. WritePathList(general_t_librarypath,librarysearchpath);
  255. WritePathList(general_t_objectpath,objectsearchpath);
  256. {$ifdef TP}
  257. {$ifndef Delphi}
  258. Comment(V_Info,'Memory: '+tostr(MemAvail)+' Bytes Free');
  259. {$endif Delphi}
  260. {$endif}
  261. {$ifdef USEEXCEPT}
  262. if setjmp(recoverpos)=0 then
  263. begin
  264. recoverpospointer:=@recoverpos;
  265. {$ifdef TP}
  266. do_stop:=recoverstop;
  267. {$else TP}
  268. do_stop:=@recoverstop;
  269. {$endif TP}
  270. {$endif USEEXCEPT}
  271. starttime:=getrealtime;
  272. if parapreprocess then
  273. parser.preprocess(inputdir+inputfile+inputextension)
  274. else
  275. parser.compile(inputdir+inputfile+inputextension,false);
  276. if status.errorcount=0 then
  277. begin
  278. starttime:=getrealtime-starttime;
  279. if starttime<0 then
  280. starttime:=starttime+3600.0*24.0;
  281. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  282. '.'+tostr(trunc(frac(starttime)*10)));
  283. end;
  284. {$ifdef USEEXCEPT}
  285. end;
  286. {$endif USEEXCEPT}
  287. { Stop is always called, so we come here when a program is compiled or not }
  288. do_stop:=olddo_stop;
  289. { Stop the compiler, frees also memory }
  290. { no message possible after this !! }
  291. DoneCompiler;
  292. { Set the return value if an error has occurred }
  293. if status.errorcount=0 then
  294. Compile:=0
  295. else
  296. Compile:=1;
  297. DoneVerbose;
  298. {$ifdef EXTDEBUG}
  299. {$ifdef FPC}
  300. LostMemory:=system.HeapSize-MemAvail-EntryMemUsed;
  301. CheckMemory(LostMemory);
  302. {$endif FPC}
  303. {$ifndef newcg}
  304. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  305. {$endif newcg}
  306. {$endif EXTDEBUG}
  307. {$ifdef fixLeaksOnError}
  308. {$ifdef tp}
  309. do_stop;
  310. {$else tp}
  311. do_stop();
  312. {$endif tp}
  313. {$endif fixLeaksOnError}
  314. end;
  315. end.
  316. {
  317. $Log$
  318. Revision 1.49 2000-05-03 16:31:22 pierre
  319. + easier debug when memory is lost
  320. Revision 1.48 2000/04/05 21:18:04 pierre
  321. * set NOUSEEXCEPT to remove use of setjump/longjump
  322. Revision 1.47 2000/03/18 15:05:33 jonas
  323. + added $maxfpuregisters 0 for compile() procedure
  324. Revision 1.46 2000/02/09 13:22:50 peter
  325. * log truncated
  326. Revision 1.45 2000/01/11 17:16:04 jonas
  327. * removed a lot of memory leaks when an error is encountered (caused by
  328. procinfo and pstringcontainers). There are still plenty left though :)
  329. Revision 1.44 2000/01/11 16:56:22 jonas
  330. - removed call to do_stop at the end of compile() since it obviously breaks the
  331. automatic compiling of units. Make cycle worked though! 8)
  332. Revision 1.43 2000/01/11 16:53:24 jonas
  333. + call do_stop at the end of compile()
  334. Revision 1.42 2000/01/07 01:14:23 peter
  335. * updated copyright to 2000
  336. Revision 1.41 1999/12/02 17:34:34 peter
  337. * preprocessor support. But it fails on the caret in type blocks
  338. Revision 1.40 1999/11/18 13:43:48 pierre
  339. + IsExe global var needed for IDE
  340. Revision 1.39 1999/11/12 11:03:50 peter
  341. * searchpaths changed to stringqueue object
  342. Revision 1.38 1999/11/09 23:47:53 pierre
  343. + minimal_stop to avoid memory loss with -iTO switch
  344. Revision 1.37 1999/11/06 14:34:20 peter
  345. * truncated log to 20 revs
  346. Revision 1.36 1999/10/12 21:20:41 florian
  347. * new codegenerator compiles again
  348. Revision 1.35 1999/09/28 19:48:45 florian
  349. * bug 617 fixed
  350. Revision 1.34 1999/09/16 23:05:52 florian
  351. * m68k compiler is again compilable (only gas writer, no assembler reader)
  352. Revision 1.33 1999/09/07 15:10:04 pierre
  353. * use do_halt instead of halt
  354. Revision 1.32 1999/09/02 18:47:44 daniel
  355. * Could not compile with TP, some arrays moved to heap
  356. * NOAG386BIN default for TP
  357. * AG386* files were not compatible with TP, fixed.
  358. Revision 1.31 1999/08/20 10:17:01 michael
  359. + Patch from pierre
  360. Revision 1.30 1999/08/11 17:26:31 peter
  361. * tlinker object is now inherited for win32 and dos
  362. * postprocessexecutable is now a method of tlinker
  363. Revision 1.29 1999/08/09 22:13:43 peter
  364. * fixed writing of lost memory which should be after donecompiler
  365. Revision 1.28 1999/08/04 13:02:40 jonas
  366. * all tokens now start with an underscore
  367. * PowerPC compiles!!
  368. Revision 1.27 1999/08/02 21:28:56 florian
  369. * the main branch psub.pas is now used for
  370. newcg compiler
  371. Revision 1.26 1999/08/02 20:46:57 michael
  372. * Alpha aware switch detection
  373. }