compiler.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. { cpu parameter handling }
  107. ,cpupara
  108. { procinfo stuff }
  109. ,cpupi
  110. { system information for source system }
  111. { the information about the target os }
  112. { are pulled in by the t_* units }
  113. {$ifdef amiga}
  114. ,i_amiga
  115. {$endif amiga}
  116. {$ifdef atari}
  117. ,i_atari
  118. {$endif atari}
  119. {$ifdef beos}
  120. ,i_beos
  121. {$endif beos}
  122. {$ifdef fbds}
  123. ,i_fbsd
  124. {$endif fbds}
  125. {$ifdef go32v2}
  126. ,i_go32v2
  127. {$endif go32v2}
  128. {$ifdef linux}
  129. ,i_linux
  130. {$endif linux}
  131. {$ifdef macos}
  132. ,i_macos
  133. {$endif macos}
  134. {$ifdef nwm}
  135. ,i_nwm
  136. {$endif nwm}
  137. {$ifdef os2}
  138. ,i_os2
  139. {$endif os2}
  140. {$ifdef palmos}
  141. ,i_palmos
  142. {$endif palmos}
  143. {$ifdef sunos}
  144. ,i_sunos
  145. {$endif sunos}
  146. {$ifdef wdosx}
  147. ,i_wdosx
  148. {$endif wdosx}
  149. {$ifdef win32}
  150. ,i_win32
  151. {$endif win32}
  152. ;
  153. function Compile(const cmd:string):longint;
  154. implementation
  155. uses
  156. aasmcpu;
  157. var
  158. CompilerInitedAfterArgs,
  159. CompilerInited : boolean;
  160. olddo_stop : tstopprocedure;
  161. {$ifdef USEEXCEPT}
  162. procedure RecoverStop;
  163. begin
  164. if recoverpospointer<>nil then
  165. LongJmp(recoverpospointer^,1)
  166. else
  167. Do_Halt(1);
  168. end;
  169. {$endif USEEXCEPT}
  170. {$ifdef EXTDEBUG}
  171. {$ifdef FPC}
  172. Var
  173. LostMemory : longint;
  174. Procedure CheckMemory(LostMemory : longint);
  175. begin
  176. if LostMemory<>0 then
  177. begin
  178. Writeln('Memory Lost = '+tostr(LostMemory));
  179. {$ifdef DEBUG}
  180. def_gdb_stop(V_Warning);
  181. {$endif DEBUG}
  182. end;
  183. end;
  184. {$endif FPC}
  185. {$endif EXTDEBUG}
  186. {****************************************************************************
  187. Compiler
  188. ****************************************************************************}
  189. procedure DoneCompiler;
  190. begin
  191. if not CompilerInited then
  192. exit;
  193. { Free compiler if args are read }
  194. {$ifdef BrowserLog}
  195. DoneBrowserLog;
  196. {$endif BrowserLog}
  197. {$ifdef BrowserCol}
  198. do_doneSymbolInfo;
  199. {$endif BrowserCol}
  200. if CompilerInitedAfterArgs then
  201. begin
  202. CompilerInitedAfterArgs:=false;
  203. DoneParser;
  204. DoneImport;
  205. DoneExport;
  206. DoneLinker;
  207. DoneAssembler;
  208. DoneAsm;
  209. end;
  210. { Free memory for the others }
  211. CompilerInited:=false;
  212. DoneSymtable;
  213. DoneGlobals;
  214. donetokens;
  215. {$ifdef USEEXCEPT}
  216. recoverpospointer:=nil;
  217. longjump_used:=false;
  218. {$endif USEEXCEPT}
  219. end;
  220. procedure InitCompiler(const cmd:string);
  221. begin
  222. if CompilerInited then
  223. DoneCompiler;
  224. { inits which need to be done before the arguments are parsed }
  225. InitSystems;
  226. { globals depends on source_info so it must be after systems }
  227. InitGlobals;
  228. { verbose depends on exe_path and must be after globals }
  229. InitVerbose;
  230. {$ifdef BrowserLog}
  231. InitBrowserLog;
  232. {$endif BrowserLog}
  233. {$ifdef BrowserCol}
  234. do_initSymbolInfo;
  235. {$endif BrowserCol}
  236. inittokens;
  237. InitSymtable;
  238. CompilerInited:=true;
  239. { this is needed here for the IDE
  240. in case of compilation failure
  241. at the previous compile }
  242. current_module:=nil;
  243. { read the arguments }
  244. read_arguments(cmd);
  245. { inits which depend on arguments }
  246. InitParser;
  247. InitImport;
  248. InitExport;
  249. InitLinker;
  250. InitAssembler;
  251. InitAsm;
  252. CompilerInitedAfterArgs:=true;
  253. end;
  254. procedure minimal_stop;
  255. begin
  256. DoneCompiler;
  257. olddo_stop{$ifdef FPCPROCVAR}(){$endif};
  258. end;
  259. function Compile(const cmd:string):longint;
  260. {$ifdef fpc}
  261. {$maxfpuregisters 0}
  262. {$endif fpc}
  263. procedure writepathlist(w:longint;l:TSearchPathList);
  264. var
  265. hp : tstringlistitem;
  266. begin
  267. hp:=tstringlistitem(l.first);
  268. while assigned(hp) do
  269. begin
  270. Message1(w,hp.str);
  271. hp:=tstringlistitem(hp.next);
  272. end;
  273. end;
  274. function getrealtime : real;
  275. var
  276. h,m,s,s100 : word;
  277. begin
  278. gettime(h,m,s,s100);
  279. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  280. end;
  281. var
  282. starttime : real;
  283. {$ifdef USEEXCEPT}
  284. recoverpos : jmp_buf;
  285. {$endif}
  286. begin
  287. olddo_stop:=do_stop;
  288. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  289. { Initialize the compiler }
  290. InitCompiler(cmd);
  291. { show some info }
  292. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  293. Message1(general_d_sourceos,source_info.name);
  294. Message1(general_i_targetos,target_info.name);
  295. Message1(general_t_exepath,exepath);
  296. WritePathList(general_t_unitpath,unitsearchpath);
  297. WritePathList(general_t_includepath,includesearchpath);
  298. WritePathList(general_t_librarypath,librarysearchpath);
  299. WritePathList(general_t_objectpath,objectsearchpath);
  300. {$ifdef USEEXCEPT}
  301. if setjmp(recoverpos)=0 then
  302. begin
  303. recoverpospointer:=@recoverpos;
  304. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  305. {$endif USEEXCEPT}
  306. starttime:=getrealtime;
  307. {$ifdef PREPROCWRITE}
  308. if parapreprocess then
  309. parser.preprocess(inputdir+inputfile+inputextension)
  310. else
  311. {$endif PREPROCWRITE}
  312. parser.compile(inputdir+inputfile+inputextension);
  313. if status.errorcount=0 then
  314. begin
  315. starttime:=getrealtime-starttime;
  316. if starttime<0 then
  317. starttime:=starttime+3600.0*24.0;
  318. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  319. '.'+tostr(trunc(frac(starttime)*10)));
  320. end;
  321. {$ifdef USEEXCEPT}
  322. end;
  323. {$endif USEEXCEPT}
  324. { Stop is always called, so we come here when a program is compiled or not }
  325. do_stop:=olddo_stop;
  326. { Stop the compiler, frees also memory }
  327. { no message possible after this !! }
  328. DoneCompiler;
  329. { Set the return value if an error has occurred }
  330. if status.errorcount=0 then
  331. Compile:=0
  332. else
  333. Compile:=1;
  334. DoneVerbose;
  335. {$ifdef EXTDEBUG}
  336. {$ifdef FPC}
  337. LostMemory:=system.HeapSize-MemAvail-EntryMemUsed;
  338. CheckMemory(LostMemory);
  339. {$endif FPC}
  340. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  341. Writeln('Repetitive resulttypepass = ',multiresulttypepasscnt,'/',resulttypepasscnt);
  342. {$endif EXTDEBUG}
  343. {$ifdef MEMDEBUG}
  344. Writeln('Memory used: ',system.Heapsize);
  345. {$endif}
  346. {$ifdef fixLeaksOnError}
  347. do_stop{$ifdef FPCPROCVAR}(){$endif};
  348. {$endif fixLeaksOnError}
  349. end;
  350. end.
  351. {
  352. $Log$
  353. Revision 1.34 2002-08-17 09:23:34 florian
  354. * first part of procinfo rewrite
  355. Revision 1.33 2002/07/26 21:15:37 florian
  356. * rewrote the system handling
  357. Revision 1.32 2002/07/11 14:41:27 florian
  358. * start of the new generic parameter handling
  359. Revision 1.31 2002/07/04 19:00:23 florian
  360. + x86_64 define added
  361. Revision 1.30 2002/07/01 18:46:22 peter
  362. * internal linker
  363. * reorganized aasm layer
  364. Revision 1.29 2002/05/18 13:34:06 peter
  365. * readded missing revisions
  366. Revision 1.28 2002/05/16 19:46:35 carl
  367. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  368. + try to fix temp allocation (still in ifdef)
  369. + generic constructor calls
  370. + start of tassembler / tmodulebase class cleanup
  371. Revision 1.26 2002/05/12 16:53:05 peter
  372. * moved entry and exitcode to ncgutil and cgobj
  373. * foreach gets extra argument for passing local data to the
  374. iterator function
  375. * -CR checks also class typecasts at runtime by changing them
  376. into as
  377. * fixed compiler to cycle with the -CR option
  378. * fixed stabs with elf writer, finally the global variables can
  379. be watched
  380. * removed a lot of routines from cga unit and replaced them by
  381. calls to cgobj
  382. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  383. u32bit then the other is typecasted also to u32bit without giving
  384. a rangecheck warning/error.
  385. * fixed pascal calling method with reversing also the high tree in
  386. the parast, detected by tcalcst3 test
  387. Revision 1.25 2002/04/15 19:53:54 peter
  388. * fixed conflicts between the last 2 commits
  389. Revision 1.24 2002/04/15 18:56:42 carl
  390. + InitAsm
  391. Revision 1.23 2002/03/24 19:05:31 carl
  392. + patch for SPARC from Mazen NEIFER
  393. }