compiler.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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. {
  19. possible compiler switches:
  20. -----------------------------------------------------------------
  21. TP to compile the compiler with Turbo or Borland Pascal
  22. I386 generate a compiler for the Intel i386+
  23. M68K generate a compiler for the M68000
  24. GDB support of the GNU Debugger
  25. EXTDEBUG some extra debug code is executed
  26. SUPPORT_MMX only i386: releases the compiler switch
  27. MMX which allows the compiler to generate
  28. MMX instructions
  29. EXTERN_MSG Don't compile the msgfiles in the compiler, always
  30. use external messagefiles
  31. NOAG386INT no Intel Assembler output
  32. NOAG386NSM no NASM output
  33. -----------------------------------------------------------------
  34. }
  35. {$ifdef FPC}
  36. { but I386 or M68K must be defined }
  37. { and only one of the two }
  38. {$ifndef I386}
  39. {$ifndef M68K}
  40. {$fatal One of the switches I386 or M68K must be defined}
  41. {$endif M68K}
  42. {$endif I386}
  43. {$ifdef I386}
  44. {$ifdef M68K}
  45. {$fatal ONLY one of the switches I386 or M68K must be defined}
  46. {$endif M68K}
  47. {$endif I386}
  48. {$ifdef support_mmx}
  49. {$ifndef i386}
  50. {$fatal I386 switch must be on for MMX support}
  51. {$endif i386}
  52. {$endif support_mmx}
  53. {$endif}
  54. unit compiler;
  55. interface
  56. { Use exception catching so the compiler goes futher after a Stop }
  57. {$ifdef i386}
  58. {$define USEEXCEPT}
  59. {$endif}
  60. {$ifdef TP}
  61. {$ifdef DPMI}
  62. {$undef USEEXCEPT}
  63. {$endif}
  64. {$endif}
  65. uses
  66. {$ifdef fpc}
  67. {$ifdef GO32V2}
  68. emu387,
  69. { dpmiexcp, }
  70. {$endif GO32V2}
  71. {$ifdef LINUX}
  72. catch,
  73. {$endif LINUX}
  74. {$endif}
  75. {$ifdef USEEXCEPT}
  76. tpexcept,
  77. {$endif USEEXCEPT}
  78. {$ifdef BrowserLog}
  79. browlog,
  80. {$endif BrowserLog}
  81. {$ifdef BrowserCol}
  82. browcol,
  83. {$endif BrowserCol}
  84. {$ifdef Delphi}
  85. dmisc,
  86. {$else Delphi}
  87. dos,
  88. {$endif Delphi}
  89. verbose,comphook,systems,
  90. globals,options,parser,symtable,link,import,export;
  91. function Compile(const cmd:string):longint;
  92. implementation
  93. var
  94. CompilerInitedAfterArgs,
  95. CompilerInited : boolean;
  96. {$ifdef USEEXCEPT}
  97. procedure RecoverStop;{$ifndef FPC}far;{$endif}
  98. begin
  99. if recoverpospointer<>nil then
  100. LongJmp(recoverpospointer^,1)
  101. else
  102. Halt(1);
  103. end;
  104. {$endif USEEXCEPT}
  105. {****************************************************************************
  106. Compiler
  107. ****************************************************************************}
  108. procedure DoneCompiler;
  109. begin
  110. if not CompilerInited then
  111. exit;
  112. { Free compiler if args are read }
  113. {$ifdef BrowserLog}
  114. DoneBrowserLog;
  115. {$endif BrowserLog}
  116. {$ifdef BrowserCol}
  117. DoneBrowserCol;
  118. {$endif BrowserCol}
  119. if CompilerInitedAfterArgs then
  120. begin
  121. CompilerInitedAfterArgs:=false;
  122. doneparser;
  123. DoneImport;
  124. DoneExport;
  125. end;
  126. { Free memory for the others }
  127. CompilerInited:=false;
  128. DoneSymtable;
  129. DoneGlobals;
  130. linker.done;
  131. {$ifdef USEEXCEPT}
  132. recoverpospointer:=nil;
  133. longjump_used:=false;
  134. {$endif USEEXCEPT}
  135. end;
  136. procedure InitCompiler(const cmd:string);
  137. begin
  138. if CompilerInited then
  139. DoneCompiler;
  140. { inits which need to be done before the arguments are parsed }
  141. InitSystems;
  142. InitVerbose;
  143. {$ifdef BrowserLog}
  144. InitBrowserLog;
  145. {$endif BrowserLog}
  146. {$ifdef BrowserCol}
  147. InitBrowserCol;
  148. {$endif BrowserCol}
  149. InitGlobals;
  150. InitSymtable;
  151. linker.init;
  152. CompilerInited:=true;
  153. { read the arguments }
  154. read_arguments(cmd);
  155. { inits which depend on arguments }
  156. initparser;
  157. InitImport;
  158. InitExport;
  159. CompilerInitedAfterArgs:=true;
  160. end;
  161. function Compile(const cmd:string):longint;
  162. function getrealtime : real;
  163. var
  164. h,m,s,s100 : word;
  165. begin
  166. gettime(h,m,s,s100);
  167. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  168. end;
  169. var
  170. starttime : real;
  171. {$ifdef USEEXCEPT}
  172. recoverpos : jmp_buf;
  173. olddo_stop : tstopprocedure;
  174. {$endif}
  175. {$IfDef Extdebug}
  176. {$ifdef FPC}
  177. EntryMemUsed : longint;
  178. {$endif FPC}
  179. {$EndIf}
  180. begin
  181. {$ifdef EXTDEBUG}
  182. {$ifdef FPC}
  183. EntryMemUsed:=system.HeapSize-MemAvail;
  184. {$endif FPC}
  185. {$endif}
  186. { Initialize the compiler }
  187. InitCompiler(cmd);
  188. { show some info }
  189. Message1(general_t_compilername,FixFileName(paramstr(0)));
  190. Message1(general_d_sourceos,source_os.name);
  191. Message1(general_i_targetos,target_os.name);
  192. Message1(general_t_exepath,exepath);
  193. Message1(general_t_unitpath,unitsearchpath);
  194. Message1(general_t_includepath,includesearchpath);
  195. Message1(general_t_librarypath,Linker.librarysearchpath);
  196. Message1(general_t_objectpath,objectsearchpath);
  197. {$ifdef TP}
  198. {$ifndef Delphi}
  199. Comment(V_Info,'Memory: '+tostr(MemAvail)+' Bytes Free');
  200. {$endif Delphi}
  201. {$endif}
  202. {$ifdef USEEXCEPT}
  203. if setjmp(recoverpos)=0 then
  204. begin
  205. olddo_stop:=do_stop;
  206. recoverpospointer:=@recoverpos;
  207. {$ifdef TP}
  208. do_stop:=recoverstop;
  209. {$else TP}
  210. do_stop:=@recoverstop;
  211. {$endif TP}
  212. {$endif USEEXCEPT}
  213. starttime:=getrealtime;
  214. parser.compile(inputdir+inputfile+inputextension,false);
  215. if status.errorcount=0 then
  216. begin
  217. starttime:=getrealtime-starttime;
  218. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  219. '.'+tostr(trunc(frac(starttime)*10)));
  220. end;
  221. {$ifdef USEEXCEPT}
  222. end;
  223. { Stop is always called, so we come here when a program is compiled or not }
  224. do_stop:=olddo_stop;
  225. {$endif USEEXCEPT}
  226. {$ifdef EXTDEBUG}
  227. {$ifdef FPC}
  228. Comment(V_Info,'Memory Lost = '+tostr(system.HeapSize-MemAvail+EntryMemUsed));
  229. {$endif FPC}
  230. Comment(V_Info,'Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  231. {$endif EXTDEBUG}
  232. { Stop the compiler, frees also memory }
  233. { no message possible after this !! }
  234. DoneCompiler;
  235. { Set the return value if an error has occurred }
  236. if status.errorcount=0 then
  237. Compile:=0
  238. else
  239. Compile:=1;
  240. DoneVerbose;
  241. end;
  242. end.
  243. {
  244. $Log$
  245. Revision 1.25 1999-07-18 14:47:22 florian
  246. * bug 487 fixed, (inc(<property>) isn't allowed)
  247. * more fixes to compile with Delphi
  248. Revision 1.24 1999/07/18 10:19:48 florian
  249. * made it compilable with Dlephi 4 again
  250. + fixed problem with large stack allocations on win32
  251. Revision 1.23 1999/06/22 16:24:41 pierre
  252. * local browser stuff corrected
  253. Revision 1.22 1999/05/17 14:24:32 pierre
  254. * DoneCompiler called later to prevent accessing invalid data
  255. Revision 1.21 1999/05/04 21:44:39 florian
  256. * changes to compile it with Delphi 4.0
  257. Revision 1.20 1999/04/21 09:43:33 peter
  258. * storenumber works
  259. * fixed some typos in double_checksum
  260. + incompatible types type1 and type2 message (with storenumber)
  261. Revision 1.19 1999/03/09 11:52:06 pierre
  262. * compilation after a failure longjumped directly to end
  263. Revision 1.18 1999/02/26 00:48:16 peter
  264. * assembler writers fixed for ag386bin
  265. Revision 1.17 1999/01/12 14:25:25 peter
  266. + BrowserLog for browser.log generation
  267. + BrowserCol for browser info in TCollections
  268. * released all other UseBrowser
  269. Revision 1.16 1998/12/15 10:23:23 peter
  270. + -iSO, -iSP, -iTO, -iTP
  271. Revision 1.15 1998/10/29 11:35:40 florian
  272. * some dll support for win32
  273. * fixed assembler writing for PalmOS
  274. Revision 1.14 1998/10/26 22:58:17 florian
  275. * new introduded problem with classes fix, the parent class wasn't set
  276. correct, if the class was defined forward before
  277. Revision 1.13 1998/10/26 17:15:17 pierre
  278. + added two level of longjump to
  279. allow clean freeing of used memory on errors
  280. Revision 1.12 1998/10/09 16:36:02 pierre
  281. * some memory leaks specific to usebrowser define fixed
  282. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  283. Revision 1.11 1998/10/08 23:28:51 peter
  284. * -vu shows unit info, -vt shows tried/used files
  285. Revision 1.10 1998/10/08 17:17:18 pierre
  286. * current_module old scanner tagged as invalid if unit is recompiled
  287. + added ppheap for better info on tracegetmem of heaptrc
  288. (adds line column and file index)
  289. * several memory leaks removed ith help of heaptrc !!
  290. Revision 1.9 1998/10/06 17:16:46 pierre
  291. * some memory leaks fixed (thanks to Peter for heaptrc !)
  292. Revision 1.8 1998/09/01 09:00:27 peter
  293. - removed tempheap creation/restore
  294. Revision 1.7 1998/09/01 07:54:17 pierre
  295. * UseBrowser a little updated (might still be buggy !!)
  296. * bug in psub.pas in function specifier removed
  297. * stdcall allowed in interface and in implementation
  298. (FPC will not yet complain if it is missing in either part
  299. because stdcall is only a dummy !!)
  300. Revision 1.6 1998/08/29 13:51:10 peter
  301. * moved get_exepath to globals
  302. + date_string const with the current date for 0.99.7+
  303. Revision 1.5 1998/08/11 21:38:24 peter
  304. + createheap/restoreheap procedures (only tp7 rm currently) and support
  305. for tp7 dpmi
  306. Revision 1.4 1998/08/11 14:09:06 peter
  307. * fixed some messages and smaller msgtxt.inc
  308. Revision 1.3 1998/08/11 00:01:20 peter
  309. * -vu displays now all searchpaths
  310. Revision 1.2 1998/08/10 14:49:56 peter
  311. + localswitches, moduleswitches, globalswitches splitting
  312. Revision 1.1 1998/08/10 10:18:24 peter
  313. + Compiler,Comphook unit which are the new interface units to the
  314. compiler
  315. }