compiler.pas 9.1 KB

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