compiler.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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. {$ifdef SPARC}
  55. {$ifndef CPUOK}
  56. {$DEFINE CPUOK}
  57. {$else}
  58. {$fatal cannot define two CPU switches}
  59. {$endif}
  60. {$endif}
  61. {$ifndef CPUOK}
  62. {$fatal One of the switches I386, iA64, Alpha, PowerPC or M68K must be defined}
  63. {$endif}
  64. {$ifdef support_mmx}
  65. {$ifndef i386}
  66. {$fatal I386 switch must be on for MMX support}
  67. {$endif i386}
  68. {$endif support_mmx}
  69. {$endif}
  70. interface
  71. uses
  72. {$ifdef fpc}
  73. {$ifdef GO32V2}
  74. emu387,
  75. {$endif GO32V2}
  76. {$endif}
  77. {$ifdef USEEXCEPT}
  78. tpexcept,
  79. {$endif USEEXCEPT}
  80. {$ifdef BrowserLog}
  81. browlog,
  82. {$endif BrowserLog}
  83. {$ifdef Delphi}
  84. dmisc,
  85. {$else Delphi}
  86. dos,
  87. {$endif Delphi}
  88. verbose,comphook,systems,
  89. cutils,cclasses,globals,options,fmodule,parser,symtable,
  90. assemble,link,import,export,tokens,pass_1
  91. { cpu overrides }
  92. ,cpuswtch
  93. { cpu codegenerator }
  94. {$ifndef NOPASS2}
  95. ,cpunode
  96. {$endif}
  97. { cpu targets }
  98. ,cputarg
  99. ;
  100. function Compile(const cmd:string):longint;
  101. implementation
  102. uses
  103. aasmcpu;
  104. var
  105. CompilerInitedAfterArgs,
  106. CompilerInited : boolean;
  107. olddo_stop : tstopprocedure;
  108. {$ifdef USEEXCEPT}
  109. procedure RecoverStop;
  110. begin
  111. if recoverpospointer<>nil then
  112. LongJmp(recoverpospointer^,1)
  113. else
  114. Do_Halt(1);
  115. end;
  116. {$endif USEEXCEPT}
  117. {$ifdef EXTDEBUG}
  118. {$ifdef FPC}
  119. Var
  120. LostMemory : longint;
  121. Procedure CheckMemory(LostMemory : longint);
  122. begin
  123. if LostMemory<>0 then
  124. begin
  125. Writeln('Memory Lost = '+tostr(LostMemory));
  126. {$ifdef DEBUG}
  127. def_gdb_stop(V_Warning);
  128. {$endif DEBUG}
  129. end;
  130. end;
  131. {$endif FPC}
  132. {$endif EXTDEBUG}
  133. {****************************************************************************
  134. Compiler
  135. ****************************************************************************}
  136. procedure DoneCompiler;
  137. begin
  138. if not CompilerInited then
  139. exit;
  140. { Free compiler if args are read }
  141. {$ifdef BrowserLog}
  142. DoneBrowserLog;
  143. {$endif BrowserLog}
  144. {$ifdef BrowserCol}
  145. do_doneSymbolInfo;
  146. {$endif BrowserCol}
  147. if CompilerInitedAfterArgs then
  148. begin
  149. CompilerInitedAfterArgs:=false;
  150. DoneParser;
  151. DoneImport;
  152. DoneExport;
  153. DoneLinker;
  154. DoneAssembler;
  155. DoneAsm;
  156. end;
  157. { Free memory for the others }
  158. CompilerInited:=false;
  159. DoneSymtable;
  160. DoneGlobals;
  161. donetokens;
  162. {$ifdef USEEXCEPT}
  163. recoverpospointer:=nil;
  164. longjump_used:=false;
  165. {$endif USEEXCEPT}
  166. end;
  167. procedure InitCompiler(const cmd:string);
  168. begin
  169. if CompilerInited then
  170. DoneCompiler;
  171. { inits which need to be done before the arguments are parsed }
  172. InitSystems;
  173. { globals depends on source_info so it must be after systems }
  174. InitGlobals;
  175. { verbose depends on exe_path and must be after globals }
  176. InitVerbose;
  177. {$ifdef BrowserLog}
  178. InitBrowserLog;
  179. {$endif BrowserLog}
  180. {$ifdef BrowserCol}
  181. do_initSymbolInfo;
  182. {$endif BrowserCol}
  183. inittokens;
  184. InitSymtable;
  185. CompilerInited:=true;
  186. { this is needed here for the IDE
  187. in case of compilation failure
  188. at the previous compile }
  189. current_module:=nil;
  190. { read the arguments }
  191. read_arguments(cmd);
  192. { inits which depend on arguments }
  193. InitParser;
  194. InitImport;
  195. InitExport;
  196. InitLinker;
  197. InitAssembler;
  198. InitAsm;
  199. CompilerInitedAfterArgs:=true;
  200. end;
  201. procedure minimal_stop;
  202. begin
  203. DoneCompiler;
  204. olddo_stop{$ifdef FPCPROCVAR}(){$endif};
  205. end;
  206. function Compile(const cmd:string):longint;
  207. {$ifdef fpc}
  208. {$maxfpuregisters 0}
  209. {$endif fpc}
  210. procedure writepathlist(w:longint;l:TSearchPathList);
  211. var
  212. hp : tstringlistitem;
  213. begin
  214. hp:=tstringlistitem(l.first);
  215. while assigned(hp) do
  216. begin
  217. Message1(w,hp.str);
  218. hp:=tstringlistitem(hp.next);
  219. end;
  220. end;
  221. function getrealtime : real;
  222. var
  223. h,m,s,s100 : word;
  224. begin
  225. gettime(h,m,s,s100);
  226. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  227. end;
  228. var
  229. starttime : real;
  230. {$ifdef USEEXCEPT}
  231. recoverpos : jmp_buf;
  232. {$endif}
  233. begin
  234. olddo_stop:=do_stop;
  235. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  236. { Initialize the compiler }
  237. InitCompiler(cmd);
  238. { show some info }
  239. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  240. Message1(general_d_sourceos,source_info.name);
  241. Message1(general_i_targetos,target_info.name);
  242. Message1(general_t_exepath,exepath);
  243. WritePathList(general_t_unitpath,unitsearchpath);
  244. WritePathList(general_t_includepath,includesearchpath);
  245. WritePathList(general_t_librarypath,librarysearchpath);
  246. WritePathList(general_t_objectpath,objectsearchpath);
  247. {$ifdef USEEXCEPT}
  248. if setjmp(recoverpos)=0 then
  249. begin
  250. recoverpospointer:=@recoverpos;
  251. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  252. {$endif USEEXCEPT}
  253. starttime:=getrealtime;
  254. {$ifdef PREPROCWRITE}
  255. if parapreprocess then
  256. parser.preprocess(inputdir+inputfile+inputextension)
  257. else
  258. {$endif PREPROCWRITE}
  259. parser.compile(inputdir+inputfile+inputextension);
  260. if status.errorcount=0 then
  261. begin
  262. starttime:=getrealtime-starttime;
  263. if starttime<0 then
  264. starttime:=starttime+3600.0*24.0;
  265. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  266. '.'+tostr(trunc(frac(starttime)*10)));
  267. end;
  268. {$ifdef USEEXCEPT}
  269. end;
  270. {$endif USEEXCEPT}
  271. { Stop is always called, so we come here when a program is compiled or not }
  272. do_stop:=olddo_stop;
  273. { Stop the compiler, frees also memory }
  274. { no message possible after this !! }
  275. DoneCompiler;
  276. { Set the return value if an error has occurred }
  277. if status.errorcount=0 then
  278. Compile:=0
  279. else
  280. Compile:=1;
  281. DoneVerbose;
  282. {$ifdef EXTDEBUG}
  283. {$ifdef FPC}
  284. LostMemory:=system.HeapSize-MemAvail-EntryMemUsed;
  285. CheckMemory(LostMemory);
  286. {$endif FPC}
  287. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  288. Writeln('Repetitive resulttypepass = ',multiresulttypepasscnt,'/',resulttypepasscnt);
  289. {$endif EXTDEBUG}
  290. {$ifdef MEMDEBUG}
  291. Writeln('Memory used: ',system.Heapsize);
  292. {$endif}
  293. {$ifdef fixLeaksOnError}
  294. do_stop{$ifdef FPCPROCVAR}(){$endif};
  295. {$endif fixLeaksOnError}
  296. end;
  297. end.
  298. {
  299. $Log$
  300. Revision 1.30 2002-07-01 18:46:22 peter
  301. * internal linker
  302. * reorganized aasm layer
  303. Revision 1.29 2002/05/18 13:34:06 peter
  304. * readded missing revisions
  305. Revision 1.28 2002/05/16 19:46:35 carl
  306. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  307. + try to fix temp allocation (still in ifdef)
  308. + generic constructor calls
  309. + start of tassembler / tmodulebase class cleanup
  310. Revision 1.26 2002/05/12 16:53:05 peter
  311. * moved entry and exitcode to ncgutil and cgobj
  312. * foreach gets extra argument for passing local data to the
  313. iterator function
  314. * -CR checks also class typecasts at runtime by changing them
  315. into as
  316. * fixed compiler to cycle with the -CR option
  317. * fixed stabs with elf writer, finally the global variables can
  318. be watched
  319. * removed a lot of routines from cga unit and replaced them by
  320. calls to cgobj
  321. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  322. u32bit then the other is typecasted also to u32bit without giving
  323. a rangecheck warning/error.
  324. * fixed pascal calling method with reversing also the high tree in
  325. the parast, detected by tcalcst3 test
  326. Revision 1.25 2002/04/15 19:53:54 peter
  327. * fixed conflicts between the last 2 commits
  328. Revision 1.24 2002/04/15 18:56:42 carl
  329. + InitAsm
  330. Revision 1.23 2002/03/24 19:05:31 carl
  331. + patch for SPARC from Mazen NEIFER
  332. }