compiler.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. unit compiler;
  19. {$i defines.inc}
  20. {$ifdef FPC}
  21. { One of Alpha, I386 or M68K must be defined }
  22. {$UNDEF CPUOK}
  23. {$ifdef I386}
  24. {$define CPUOK}
  25. {$endif}
  26. {$ifdef M68K}
  27. {$ifndef CPUOK}
  28. {$DEFINE CPUOK}
  29. {$else}
  30. {$fatal cannot define two CPU switches}
  31. {$endif}
  32. {$endif}
  33. {$ifdef alpha}
  34. {$ifndef CPUOK}
  35. {$DEFINE CPUOK}
  36. {$else}
  37. {$fatal cannot define two CPU switches}
  38. {$endif}
  39. {$endif}
  40. {$ifdef powerpc}
  41. {$ifndef CPUOK}
  42. {$DEFINE CPUOK}
  43. {$else}
  44. {$fatal cannot define two CPU switches}
  45. {$endif}
  46. {$endif}
  47. {$ifndef CPUOK}
  48. {$fatal One of the switches I386, Alpha, PowerPC or M68K must be defined}
  49. {$endif}
  50. {$ifdef support_mmx}
  51. {$ifndef i386}
  52. {$fatal I386 switch must be on for MMX support}
  53. {$endif i386}
  54. {$endif support_mmx}
  55. {$endif}
  56. interface
  57. uses
  58. {$ifdef fpc}
  59. {$ifdef GO32V2}
  60. emu387,
  61. {$endif GO32V2}
  62. {$endif}
  63. {$ifdef USEEXCEPT}
  64. tpexcept,
  65. {$endif USEEXCEPT}
  66. {$ifdef BrowserLog}
  67. browlog,
  68. {$endif BrowserLog}
  69. {$ifdef Delphi}
  70. dmisc,
  71. {$else Delphi}
  72. dos,
  73. {$endif Delphi}
  74. verbose,comphook,systems,
  75. cutils,cobjects,globals,options,parser,symtable,link,import,export,tokens;
  76. function Compile(const cmd:string):longint;
  77. implementation
  78. uses
  79. cpubase;
  80. var
  81. CompilerInitedAfterArgs,
  82. CompilerInited : boolean;
  83. olddo_stop : tstopprocedure;
  84. {$ifdef USEEXCEPT}
  85. procedure RecoverStop;
  86. begin
  87. if recoverpospointer<>nil then
  88. LongJmp(recoverpospointer^,1)
  89. else
  90. Do_Halt(1);
  91. end;
  92. {$endif USEEXCEPT}
  93. {$ifdef EXTDEBUG}
  94. {$ifdef FPC}
  95. Var
  96. LostMemory : longint;
  97. Procedure CheckMemory(LostMemory : longint);
  98. begin
  99. if LostMemory<>0 then
  100. begin
  101. Writeln('Memory Lost = '+tostr(LostMemory));
  102. {$ifdef DEBUG}
  103. def_gdb_stop(V_Warning);
  104. {$endif DEBUG}
  105. end;
  106. end;
  107. {$endif FPC}
  108. {$endif EXTDEBUG}
  109. {****************************************************************************
  110. Compiler
  111. ****************************************************************************}
  112. procedure DoneCompiler;
  113. begin
  114. if not CompilerInited then
  115. exit;
  116. { Free compiler if args are read }
  117. {$ifdef BrowserLog}
  118. DoneBrowserLog;
  119. {$endif BrowserLog}
  120. {$ifdef BrowserCol}
  121. do_doneSymbolInfo;
  122. {$endif BrowserCol}
  123. if CompilerInitedAfterArgs then
  124. begin
  125. CompilerInitedAfterArgs:=false;
  126. doneparser;
  127. DoneImport;
  128. DoneExport;
  129. DoneLinker;
  130. DoneCpu;
  131. end;
  132. { Free memory for the others }
  133. CompilerInited:=false;
  134. DoneSymtable;
  135. DoneGlobals;
  136. donetokens;
  137. {$ifdef USEEXCEPT}
  138. recoverpospointer:=nil;
  139. longjump_used:=false;
  140. {$endif USEEXCEPT}
  141. end;
  142. procedure InitCompiler(const cmd:string);
  143. begin
  144. if CompilerInited then
  145. DoneCompiler;
  146. { inits which need to be done before the arguments are parsed }
  147. InitSystems;
  148. InitVerbose;
  149. {$ifdef BrowserLog}
  150. InitBrowserLog;
  151. {$endif BrowserLog}
  152. {$ifdef BrowserCol}
  153. do_initSymbolInfo;
  154. {$endif BrowserCol}
  155. InitGlobals;
  156. inittokens;
  157. InitSymtable;
  158. CompilerInited:=true;
  159. { read the arguments }
  160. read_arguments(cmd);
  161. { inits which depend on arguments }
  162. initparser;
  163. InitImport;
  164. InitExport;
  165. InitLinker;
  166. InitCpu;
  167. CompilerInitedAfterArgs:=true;
  168. end;
  169. procedure minimal_stop;{$ifndef fpc}far;{$endif}
  170. begin
  171. DoneCompiler;
  172. olddo_stop;
  173. end;
  174. function Compile(const cmd:string):longint;
  175. {$ifdef fpc}
  176. {$maxfpuregisters 0}
  177. {$endif fpc}
  178. procedure writepathlist(w:longint;l:TSearchPathList);
  179. var
  180. hp : pstringqueueitem;
  181. begin
  182. hp:=l.first;
  183. while assigned(hp) do
  184. begin
  185. Message1(w,hp^.data^);
  186. hp:=hp^.next;
  187. end;
  188. end;
  189. function getrealtime : real;
  190. var
  191. h,m,s,s100 : word;
  192. begin
  193. gettime(h,m,s,s100);
  194. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  195. end;
  196. var
  197. starttime : real;
  198. {$ifdef USEEXCEPT}
  199. recoverpos : jmp_buf;
  200. {$endif}
  201. begin
  202. olddo_stop:=do_stop;
  203. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  204. { Initialize the compiler }
  205. InitCompiler(cmd);
  206. { show some info }
  207. Message1(general_t_compilername,FixFileName(paramstr(0)));
  208. Message1(general_d_sourceos,source_os.name);
  209. Message1(general_i_targetos,target_os.name);
  210. Message1(general_t_exepath,exepath);
  211. WritePathList(general_t_unitpath,unitsearchpath);
  212. WritePathList(general_t_includepath,includesearchpath);
  213. WritePathList(general_t_librarypath,librarysearchpath);
  214. WritePathList(general_t_objectpath,objectsearchpath);
  215. {$ifdef USEEXCEPT}
  216. if setjmp(recoverpos)=0 then
  217. begin
  218. recoverpospointer:=@recoverpos;
  219. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  220. {$endif USEEXCEPT}
  221. starttime:=getrealtime;
  222. if parapreprocess then
  223. parser.preprocess(inputdir+inputfile+inputextension)
  224. else
  225. parser.compile(inputdir+inputfile+inputextension,false);
  226. if status.errorcount=0 then
  227. begin
  228. starttime:=getrealtime-starttime;
  229. if starttime<0 then
  230. starttime:=starttime+3600.0*24.0;
  231. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  232. '.'+tostr(trunc(frac(starttime)*10)));
  233. end;
  234. {$ifdef USEEXCEPT}
  235. end;
  236. {$endif USEEXCEPT}
  237. { Stop is always called, so we come here when a program is compiled or not }
  238. do_stop:=olddo_stop;
  239. { Stop the compiler, frees also memory }
  240. { no message possible after this !! }
  241. DoneCompiler;
  242. { Set the return value if an error has occurred }
  243. if status.errorcount=0 then
  244. Compile:=0
  245. else
  246. Compile:=1;
  247. DoneVerbose;
  248. {$ifdef EXTDEBUG}
  249. {$ifdef FPC}
  250. LostMemory:=system.HeapSize-MemAvail-EntryMemUsed;
  251. CheckMemory(LostMemory);
  252. {$endif FPC}
  253. {$ifndef newcg}
  254. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  255. {$endif newcg}
  256. {$endif EXTDEBUG}
  257. {$ifdef MEMDEBUG}
  258. Writeln('Memory used: ',system.Heapsize);
  259. {$endif}
  260. {$ifdef fixLeaksOnError}
  261. do_stop{$ifdef FPCPROCVAR}(){$endif};
  262. {$endif fixLeaksOnError}
  263. end;
  264. end.
  265. {
  266. $Log$
  267. Revision 1.6 2000-09-24 15:06:14 peter
  268. * use defines.inc
  269. Revision 1.5 2000/08/27 16:11:50 peter
  270. * moved some util functions from globals,cobjects to cutils
  271. * splitted files into finput,fmodule
  272. Revision 1.4 2000/08/21 09:14:40 jonas
  273. - removed catch unit from uses clause for Linux (clashed with fpcatch
  274. from IDE and is already in pp.pas for command line compiler) (merged
  275. from fixes branch)
  276. Revision 1.3 2000/08/04 22:00:50 peter
  277. * merges from fixes
  278. Revision 1.2 2000/07/13 11:32:38 michael
  279. + removed logs
  280. }