compiler.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. { system information for source system }
  109. { the information about the target os }
  110. { are pulled in by the t_* units }
  111. {$ifdef amiga}
  112. ,i_amiga
  113. {$endif amiga}
  114. {$ifdef atari}
  115. ,i_atari
  116. {$endif atari}
  117. {$ifdef beos}
  118. ,i_beos
  119. {$endif beos}
  120. {$ifdef fbds}
  121. ,i_fbsd
  122. {$endif fbds}
  123. {$ifdef go32v2}
  124. ,i_go32v2
  125. {$endif go32v2}
  126. {$ifdef linux}
  127. ,i_linux
  128. {$endif linux}
  129. {$ifdef macos}
  130. ,i_macos
  131. {$endif macos}
  132. {$ifdef nwm}
  133. ,i_nwm
  134. {$endif nwm}
  135. {$ifdef os2}
  136. ,i_os2
  137. {$endif os2}
  138. {$ifdef palmos}
  139. ,i_palmos
  140. {$endif palmos}
  141. {$ifdef sunos}
  142. ,i_sunos
  143. {$endif sunos}
  144. {$ifdef wdosx}
  145. ,i_wdosx
  146. {$endif wdosx}
  147. {$ifdef win32}
  148. ,i_win32
  149. {$endif win32}
  150. ;
  151. function Compile(const cmd:string):longint;
  152. implementation
  153. uses
  154. aasmcpu;
  155. var
  156. CompilerInitedAfterArgs,
  157. CompilerInited : boolean;
  158. olddo_stop : tstopprocedure;
  159. {$ifdef USEEXCEPT}
  160. procedure RecoverStop;
  161. begin
  162. if recoverpospointer<>nil then
  163. LongJmp(recoverpospointer^,1)
  164. else
  165. Do_Halt(1);
  166. end;
  167. {$endif USEEXCEPT}
  168. {$ifdef EXTDEBUG}
  169. {$ifdef FPC}
  170. Var
  171. LostMemory : longint;
  172. Procedure CheckMemory(LostMemory : longint);
  173. begin
  174. if LostMemory<>0 then
  175. begin
  176. Writeln('Memory Lost = '+tostr(LostMemory));
  177. {$ifdef DEBUG}
  178. def_gdb_stop(V_Warning);
  179. {$endif DEBUG}
  180. end;
  181. end;
  182. {$endif FPC}
  183. {$endif EXTDEBUG}
  184. {****************************************************************************
  185. Compiler
  186. ****************************************************************************}
  187. procedure DoneCompiler;
  188. begin
  189. if not CompilerInited then
  190. exit;
  191. { Free compiler if args are read }
  192. {$ifdef BrowserLog}
  193. DoneBrowserLog;
  194. {$endif BrowserLog}
  195. {$ifdef BrowserCol}
  196. do_doneSymbolInfo;
  197. {$endif BrowserCol}
  198. if CompilerInitedAfterArgs then
  199. begin
  200. CompilerInitedAfterArgs:=false;
  201. DoneParser;
  202. DoneImport;
  203. DoneExport;
  204. DoneLinker;
  205. DoneAssembler;
  206. DoneAsm;
  207. end;
  208. { Free memory for the others }
  209. CompilerInited:=false;
  210. DoneSymtable;
  211. DoneGlobals;
  212. donetokens;
  213. {$ifdef USEEXCEPT}
  214. recoverpospointer:=nil;
  215. longjump_used:=false;
  216. {$endif USEEXCEPT}
  217. end;
  218. procedure InitCompiler(const cmd:string);
  219. begin
  220. if CompilerInited then
  221. DoneCompiler;
  222. { inits which need to be done before the arguments are parsed }
  223. InitSystems;
  224. { globals depends on source_info so it must be after systems }
  225. InitGlobals;
  226. { verbose depends on exe_path and must be after globals }
  227. InitVerbose;
  228. {$ifdef BrowserLog}
  229. InitBrowserLog;
  230. {$endif BrowserLog}
  231. {$ifdef BrowserCol}
  232. do_initSymbolInfo;
  233. {$endif BrowserCol}
  234. inittokens;
  235. InitSymtable;
  236. CompilerInited:=true;
  237. { this is needed here for the IDE
  238. in case of compilation failure
  239. at the previous compile }
  240. current_module:=nil;
  241. { read the arguments }
  242. read_arguments(cmd);
  243. { inits which depend on arguments }
  244. InitParser;
  245. InitImport;
  246. InitExport;
  247. InitLinker;
  248. InitAssembler;
  249. InitAsm;
  250. CompilerInitedAfterArgs:=true;
  251. end;
  252. procedure minimal_stop;
  253. begin
  254. DoneCompiler;
  255. olddo_stop{$ifdef FPCPROCVAR}(){$endif};
  256. end;
  257. function Compile(const cmd:string):longint;
  258. {$ifdef fpc}
  259. {$maxfpuregisters 0}
  260. {$endif fpc}
  261. procedure writepathlist(w:longint;l:TSearchPathList);
  262. var
  263. hp : tstringlistitem;
  264. begin
  265. hp:=tstringlistitem(l.first);
  266. while assigned(hp) do
  267. begin
  268. Message1(w,hp.str);
  269. hp:=tstringlistitem(hp.next);
  270. end;
  271. end;
  272. function getrealtime : real;
  273. var
  274. h,m,s,s100 : word;
  275. begin
  276. gettime(h,m,s,s100);
  277. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  278. end;
  279. var
  280. starttime : real;
  281. {$ifdef USEEXCEPT}
  282. recoverpos : jmp_buf;
  283. {$endif}
  284. begin
  285. olddo_stop:=do_stop;
  286. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  287. { Initialize the compiler }
  288. InitCompiler(cmd);
  289. { show some info }
  290. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  291. Message1(general_d_sourceos,source_info.name);
  292. Message1(general_i_targetos,target_info.name);
  293. Message1(general_t_exepath,exepath);
  294. WritePathList(general_t_unitpath,unitsearchpath);
  295. WritePathList(general_t_includepath,includesearchpath);
  296. WritePathList(general_t_librarypath,librarysearchpath);
  297. WritePathList(general_t_objectpath,objectsearchpath);
  298. {$ifdef USEEXCEPT}
  299. if setjmp(recoverpos)=0 then
  300. begin
  301. recoverpospointer:=@recoverpos;
  302. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  303. {$endif USEEXCEPT}
  304. starttime:=getrealtime;
  305. {$ifdef PREPROCWRITE}
  306. if parapreprocess then
  307. parser.preprocess(inputdir+inputfile+inputextension)
  308. else
  309. {$endif PREPROCWRITE}
  310. parser.compile(inputdir+inputfile+inputextension);
  311. if status.errorcount=0 then
  312. begin
  313. starttime:=getrealtime-starttime;
  314. if starttime<0 then
  315. starttime:=starttime+3600.0*24.0;
  316. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  317. '.'+tostr(trunc(frac(starttime)*10)));
  318. end;
  319. {$ifdef USEEXCEPT}
  320. end;
  321. {$endif USEEXCEPT}
  322. { Stop is always called, so we come here when a program is compiled or not }
  323. do_stop:=olddo_stop;
  324. { Stop the compiler, frees also memory }
  325. { no message possible after this !! }
  326. DoneCompiler;
  327. { Set the return value if an error has occurred }
  328. if status.errorcount=0 then
  329. Compile:=0
  330. else
  331. Compile:=1;
  332. DoneVerbose;
  333. {$ifdef EXTDEBUG}
  334. {$ifdef FPC}
  335. LostMemory:=system.HeapSize-MemAvail-EntryMemUsed;
  336. CheckMemory(LostMemory);
  337. {$endif FPC}
  338. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  339. Writeln('Repetitive resulttypepass = ',multiresulttypepasscnt,'/',resulttypepasscnt);
  340. {$endif EXTDEBUG}
  341. {$ifdef MEMDEBUG}
  342. Writeln('Memory used: ',system.Heapsize);
  343. {$endif}
  344. {$ifdef fixLeaksOnError}
  345. do_stop{$ifdef FPCPROCVAR}(){$endif};
  346. {$endif fixLeaksOnError}
  347. end;
  348. end.
  349. {
  350. $Log$
  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. }