compiler.pas 10 KB

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