compiler.pas 7.7 KB

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