compiler.pas 10 KB

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