compiler.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$ifndef CPUOK}
  62. {$fatal One of the switches I386, iA64, Alpha, PowerPC or M68K must be defined}
  63. {$endif}
  64. {$ifdef support_mmx}
  65. {$ifndef i386}
  66. {$fatal I386 switch must be on for MMX support}
  67. {$endif i386}
  68. {$endif support_mmx}
  69. {$endif}
  70. interface
  71. uses
  72. {$ifdef fpc}
  73. {$ifdef GO32V2}
  74. emu387,
  75. {$endif GO32V2}
  76. {$endif}
  77. {$ifdef USEEXCEPT}
  78. tpexcept,
  79. {$endif USEEXCEPT}
  80. {$ifdef BrowserLog}
  81. browlog,
  82. {$endif BrowserLog}
  83. {$ifdef Delphi}
  84. dmisc,
  85. {$else Delphi}
  86. dos,
  87. {$endif Delphi}
  88. verbose,comphook,systems,
  89. cutils,cclasses,globals,options,fmodule,parser,symtable,
  90. assemble,link,import,export,tokens,pass_1
  91. { cpu overrides }
  92. ,cpuswtch
  93. { cpu codegenerator }
  94. {$ifndef NOPASS2}
  95. ,cpunode
  96. {$endif}
  97. { cpu targets }
  98. ,cputarg
  99. ;
  100. function Compile(const cmd:string):longint;
  101. implementation
  102. uses
  103. cpuasm;
  104. var
  105. CompilerInitedAfterArgs,
  106. CompilerInited : boolean;
  107. olddo_stop : tstopprocedure;
  108. {$ifdef USEEXCEPT}
  109. procedure RecoverStop;
  110. begin
  111. if recoverpospointer<>nil then
  112. LongJmp(recoverpospointer^,1)
  113. else
  114. Do_Halt(1);
  115. end;
  116. {$endif USEEXCEPT}
  117. {$ifdef EXTDEBUG}
  118. {$ifdef FPC}
  119. Var
  120. LostMemory : longint;
  121. Procedure CheckMemory(LostMemory : longint);
  122. begin
  123. if LostMemory<>0 then
  124. begin
  125. Writeln('Memory Lost = '+tostr(LostMemory));
  126. {$ifdef DEBUG}
  127. def_gdb_stop(V_Warning);
  128. {$endif DEBUG}
  129. end;
  130. end;
  131. {$endif FPC}
  132. {$endif EXTDEBUG}
  133. {****************************************************************************
  134. Compiler
  135. ****************************************************************************}
  136. procedure DoneCompiler;
  137. begin
  138. if not CompilerInited then
  139. exit;
  140. { Free compiler if args are read }
  141. {$ifdef BrowserLog}
  142. DoneBrowserLog;
  143. {$endif BrowserLog}
  144. {$ifdef BrowserCol}
  145. do_doneSymbolInfo;
  146. {$endif BrowserCol}
  147. if CompilerInitedAfterArgs then
  148. begin
  149. CompilerInitedAfterArgs:=false;
  150. DoneParser;
  151. DoneImport;
  152. DoneExport;
  153. DoneLinker;
  154. DoneAssembler;
  155. DoneAsm;
  156. end;
  157. { Free memory for the others }
  158. CompilerInited:=false;
  159. DoneSymtable;
  160. DoneGlobals;
  161. donetokens;
  162. {$ifdef USEEXCEPT}
  163. recoverpospointer:=nil;
  164. longjump_used:=false;
  165. {$endif USEEXCEPT}
  166. end;
  167. procedure InitCompiler(const cmd:string);
  168. begin
  169. if CompilerInited then
  170. DoneCompiler;
  171. { inits which need to be done before the arguments are parsed }
  172. InitSystems;
  173. { globals depends on source_info so it must be after systems }
  174. InitGlobals;
  175. { verbose depends on exe_path and must be after globals }
  176. InitVerbose;
  177. {$ifdef BrowserLog}
  178. InitBrowserLog;
  179. {$endif BrowserLog}
  180. {$ifdef BrowserCol}
  181. do_initSymbolInfo;
  182. {$endif BrowserCol}
  183. inittokens;
  184. InitSymtable;
  185. CompilerInited:=true;
  186. { this is needed here for the IDE
  187. in case of compilation failure
  188. at the previous compile }
  189. current_module:=nil;
  190. { read the arguments }
  191. read_arguments(cmd);
  192. { inits which depend on arguments }
  193. InitParser;
  194. InitImport;
  195. InitExport;
  196. InitLinker;
  197. InitAssembler;
  198. InitAsm;
  199. CompilerInitedAfterArgs:=true;
  200. end;
  201. procedure minimal_stop;
  202. begin
  203. DoneCompiler;
  204. olddo_stop{$ifdef FPCPROCVAR}(){$endif};
  205. end;
  206. function Compile(const cmd:string):longint;
  207. {$ifdef fpc}
  208. {$maxfpuregisters 0}
  209. {$endif fpc}
  210. procedure writepathlist(w:longint;l:TSearchPathList);
  211. var
  212. hp : tstringlistitem;
  213. begin
  214. hp:=tstringlistitem(l.first);
  215. while assigned(hp) do
  216. begin
  217. Message1(w,hp.str);
  218. hp:=tstringlistitem(hp.next);
  219. end;
  220. end;
  221. function getrealtime : real;
  222. var
  223. h,m,s,s100 : word;
  224. begin
  225. gettime(h,m,s,s100);
  226. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  227. end;
  228. var
  229. starttime : real;
  230. {$ifdef USEEXCEPT}
  231. recoverpos : jmp_buf;
  232. {$endif}
  233. begin
  234. olddo_stop:=do_stop;
  235. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  236. { Initialize the compiler }
  237. InitCompiler(cmd);
  238. { show some info }
  239. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  240. Message1(general_d_sourceos,source_info.name);
  241. Message1(general_i_targetos,target_info.name);
  242. Message1(general_t_exepath,exepath);
  243. WritePathList(general_t_unitpath,unitsearchpath);
  244. WritePathList(general_t_includepath,includesearchpath);
  245. WritePathList(general_t_librarypath,librarysearchpath);
  246. WritePathList(general_t_objectpath,objectsearchpath);
  247. {$ifdef USEEXCEPT}
  248. if setjmp(recoverpos)=0 then
  249. begin
  250. recoverpospointer:=@recoverpos;
  251. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  252. {$endif USEEXCEPT}
  253. starttime:=getrealtime;
  254. {$ifdef PREPROCWRITE}
  255. if parapreprocess then
  256. parser.preprocess(inputdir+inputfile+inputextension)
  257. else
  258. {$endif PREPROCWRITE}
  259. parser.compile(inputdir+inputfile+inputextension);
  260. if status.errorcount=0 then
  261. begin
  262. starttime:=getrealtime-starttime;
  263. if starttime<0 then
  264. starttime:=starttime+3600.0*24.0;
  265. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  266. '.'+tostr(trunc(frac(starttime)*10)));
  267. end;
  268. {$ifdef USEEXCEPT}
  269. end;
  270. {$endif USEEXCEPT}
  271. { Stop is always called, so we come here when a program is compiled or not }
  272. do_stop:=olddo_stop;
  273. { Stop the compiler, frees also memory }
  274. { no message possible after this !! }
  275. DoneCompiler;
  276. { Set the return value if an error has occurred }
  277. if status.errorcount=0 then
  278. Compile:=0
  279. else
  280. Compile:=1;
  281. DoneVerbose;
  282. {$ifdef EXTDEBUG}
  283. {$ifdef FPC}
  284. LostMemory:=system.HeapSize-MemAvail-EntryMemUsed;
  285. CheckMemory(LostMemory);
  286. {$endif FPC}
  287. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  288. Writeln('Repetitive resulttypepass = ',multiresulttypepasscnt,'/',resulttypepasscnt);
  289. {$endif EXTDEBUG}
  290. {$ifdef MEMDEBUG}
  291. Writeln('Memory used: ',system.Heapsize);
  292. {$endif}
  293. {$ifdef fixLeaksOnError}
  294. do_stop{$ifdef FPCPROCVAR}(){$endif};
  295. {$endif fixLeaksOnError}
  296. end;
  297. end.
  298. {
  299. $Log$
  300. Revision 1.28 2002-05-16 19:46:35 carl
  301. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  302. + try to fix temp allocation (still in ifdef)
  303. + generic constructor calls
  304. + start of tassembler / tmodulebase class cleanup
  305. Revision 1.26 2002/05/12 16:53:05 peter
  306. * moved entry and exitcode to ncgutil and cgobj
  307. * foreach gets extra argument for passing local data to the
  308. iterator function
  309. * -CR checks also class typecasts at runtime by changing them
  310. into as
  311. * fixed compiler to cycle with the -CR option
  312. * fixed stabs with elf writer, finally the global variables can
  313. be watched
  314. * removed a lot of routines from cga unit and replaced them by
  315. calls to cgobj
  316. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  317. u32bit then the other is typecasted also to u32bit without giving
  318. a rangecheck warning/error.
  319. * fixed pascal calling method with reversing also the high tree in
  320. the parast, detected by tcalcst3 test
  321. Revision 1.25 2002/04/15 19:53:54 peter
  322. * fixed conflicts between the last 2 commits
  323. Revision 1.24 2002/04/15 18:56:42 carl
  324. + InitAsm
  325. Revision 1.23 2002/03/24 19:05:31 carl
  326. + patch for SPARC from Mazen NEIFER
  327. Revision 1.22 2001/09/18 11:30:47 michael
  328. * Fixes win32 linking problems with import libraries
  329. * LINKLIB Libraries are now looked for using C file extensions
  330. * get_exepath fix
  331. Revision 1.21 2001/05/06 14:49:16 peter
  332. * ppu object to class rewrite
  333. * move ppu read and write stuff to fppu
  334. Revision 1.20 2001/04/21 13:37:16 peter
  335. * made tclassheader using class of to implement cpu dependent code
  336. Revision 1.19 2001/04/18 22:01:53 peter
  337. * registration of targets and assemblers
  338. Revision 1.18 2001/04/13 18:08:36 peter
  339. * scanner object to class
  340. Revision 1.17 2001/04/13 01:22:06 peter
  341. * symtable change to classes
  342. * range check generation and errors fixed, make cycle DEBUG=1 works
  343. * memory leaks fixed
  344. Revision 1.16 2001/04/02 21:20:29 peter
  345. * resulttype rewrite
  346. Revision 1.15 2000/12/26 15:57:25 peter
  347. * use system.paramstr()
  348. Revision 1.14 2000/12/25 00:07:25 peter
  349. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  350. tlinkedlist objects)
  351. Revision 1.13 2000/12/24 12:24:38 peter
  352. * moved preprocessfile into a conditional
  353. Revision 1.11 2000/11/29 00:30:30 florian
  354. * unused units removed from uses clause
  355. * some changes for widestrings
  356. Revision 1.10 2000/10/31 22:02:46 peter
  357. * symtable splitted, no real code changes
  358. Revision 1.9 2000/10/15 09:39:36 peter
  359. * moved cpu*.pas to i386/
  360. * renamed n386 to common cpunode
  361. Revision 1.8 2000/10/14 10:14:46 peter
  362. * moehrendorf oct 2000 rewrite
  363. Revision 1.7 2000/10/08 10:26:33 peter
  364. * merged @result fix from Pierre
  365. Revision 1.6 2000/09/24 15:06:14 peter
  366. * use defines.inc
  367. Revision 1.5 2000/08/27 16:11:50 peter
  368. * moved some util functions from globals,cobjects to cutils
  369. * splitted files into finput,fmodule
  370. Revision 1.4 2000/08/21 09:14:40 jonas
  371. - removed catch unit from uses clause for Linux (clashed with fpcatch
  372. from IDE and is already in pp.pas for command line compiler) (merged
  373. from fixes branch)
  374. Revision 1.3 2000/08/04 22:00:50 peter
  375. * merges from fixes
  376. Revision 1.2 2000/07/13 11:32:38 michael
  377. + removed logs
  378. }