compiler.pas 8.4 KB

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