compiler.pas 7.5 KB

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