compiler.pas 8.8 KB

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