compiler.pas 9.5 KB

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