compiler.pas 10 KB

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