compiler.pas 7.6 KB

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