compiler.pas 9.0 KB

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