2
0

compiler.pas 9.7 KB

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