compiler.pas 8.0 KB

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