compiler.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. {
  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. {$UNDEF 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. {$ifdef powerpc}
  56. {$ifndef CPUOK}
  57. {$DEFINE CPUOK}
  58. {$else}
  59. {$fatal cannot define two CPU switches}
  60. {$endif}
  61. {$endif}
  62. {$ifndef CPUOK}
  63. {$fatal One of the switches I386, Alpha, PowerPC or M68K must be defined}
  64. {$endif}
  65. {$ifdef support_mmx}
  66. {$ifndef i386}
  67. {$fatal I386 switch must be on for MMX support}
  68. {$endif i386}
  69. {$endif support_mmx}
  70. {$endif}
  71. unit compiler;
  72. interface
  73. { Use exception catching so the compiler goes futher after a Stop }
  74. {$ifndef NOUSEEXCEPT}
  75. {$ifdef i386}
  76. {$define USEEXCEPT}
  77. {$endif}
  78. {$ifdef TP}
  79. {$ifdef DPMI}
  80. {$undef USEEXCEPT}
  81. {$endif}
  82. {$endif}
  83. {$endif ndef NOUSEEXCEPT}
  84. uses
  85. {$ifdef fpc}
  86. {$ifdef GO32V2}
  87. emu387,
  88. { dpmiexcp, }
  89. {$endif GO32V2}
  90. {$ifdef LINUX}
  91. catch,
  92. {$endif LINUX}
  93. {$endif}
  94. {$ifdef USEEXCEPT}
  95. tpexcept,
  96. {$endif USEEXCEPT}
  97. {$ifdef BrowserLog}
  98. browlog,
  99. {$endif BrowserLog}
  100. {$ifdef BrowserCol}
  101. browcol,
  102. {$endif BrowserCol}
  103. {$ifdef Delphi}
  104. dmisc,
  105. {$else Delphi}
  106. dos,
  107. {$endif Delphi}
  108. verbose,comphook,systems,
  109. cobjects,globals,options,parser,symtable,link,import,export,tokens;
  110. function Compile(const cmd:string):longint;
  111. Const
  112. { do we need to link }
  113. IsExe : boolean = false;
  114. implementation
  115. uses
  116. cpubase;
  117. var
  118. CompilerInitedAfterArgs,
  119. CompilerInited : boolean;
  120. olddo_stop : tstopprocedure;
  121. {$ifdef USEEXCEPT}
  122. procedure RecoverStop;{$ifndef FPC}far;{$endif}
  123. begin
  124. if recoverpospointer<>nil then
  125. LongJmp(recoverpospointer^,1)
  126. else
  127. Do_Halt(1);
  128. end;
  129. {$endif USEEXCEPT}
  130. {****************************************************************************
  131. Compiler
  132. ****************************************************************************}
  133. procedure DoneCompiler;
  134. begin
  135. if not CompilerInited then
  136. exit;
  137. { Free compiler if args are read }
  138. {$ifdef BrowserLog}
  139. DoneBrowserLog;
  140. {$endif BrowserLog}
  141. {$ifdef BrowserCol}
  142. DoneBrowserCol;
  143. {$endif BrowserCol}
  144. if CompilerInitedAfterArgs then
  145. begin
  146. CompilerInitedAfterArgs:=false;
  147. doneparser;
  148. DoneImport;
  149. DoneExport;
  150. DoneLinker;
  151. DoneCpu;
  152. end;
  153. { Free memory for the others }
  154. CompilerInited:=false;
  155. DoneSymtable;
  156. DoneGlobals;
  157. donetokens;
  158. {$ifdef USEEXCEPT}
  159. recoverpospointer:=nil;
  160. longjump_used:=false;
  161. {$endif USEEXCEPT}
  162. end;
  163. procedure InitCompiler(const cmd:string);
  164. begin
  165. if CompilerInited then
  166. DoneCompiler;
  167. { inits which need to be done before the arguments are parsed }
  168. InitSystems;
  169. InitVerbose;
  170. {$ifdef BrowserLog}
  171. InitBrowserLog;
  172. {$endif BrowserLog}
  173. {$ifdef BrowserCol}
  174. InitBrowserCol;
  175. {$endif BrowserCol}
  176. InitGlobals;
  177. inittokens;
  178. InitSymtable;
  179. CompilerInited:=true;
  180. { read the arguments }
  181. read_arguments(cmd);
  182. { inits which depend on arguments }
  183. initparser;
  184. InitImport;
  185. InitExport;
  186. InitLinker;
  187. InitCpu;
  188. CompilerInitedAfterArgs:=true;
  189. end;
  190. procedure minimal_stop;{$ifndef fpc}far;{$endif}
  191. begin
  192. DoneCompiler;
  193. olddo_stop;
  194. end;
  195. function Compile(const cmd:string):longint;
  196. {$ifdef fpc}
  197. {$maxfpuregisters 0}
  198. {$endif fpc}
  199. procedure writepathlist(w:tmsgconst;l:TSearchPathList);
  200. var
  201. hp : pstringqueueitem;
  202. begin
  203. hp:=l.first;
  204. while assigned(hp) do
  205. begin
  206. Message1(w,hp^.data^);
  207. hp:=hp^.next;
  208. end;
  209. end;
  210. function getrealtime : real;
  211. var
  212. h,m,s,s100 : word;
  213. begin
  214. gettime(h,m,s,s100);
  215. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  216. end;
  217. var
  218. starttime : real;
  219. {$ifdef USEEXCEPT}
  220. recoverpos : jmp_buf;
  221. {$endif}
  222. begin
  223. olddo_stop:=do_stop;
  224. {$ifdef TP}
  225. do_stop:=minimal_stop;
  226. {$else TP}
  227. do_stop:=@minimal_stop;
  228. {$endif TP}
  229. { Initialize the compiler }
  230. InitCompiler(cmd);
  231. { show some info }
  232. Message1(general_t_compilername,FixFileName(paramstr(0)));
  233. Message1(general_d_sourceos,source_os.name);
  234. Message1(general_i_targetos,target_os.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 TP}
  241. {$ifndef Delphi}
  242. Comment(V_Info,'Memory: '+tostr(MemAvail)+' Bytes Free');
  243. {$endif Delphi}
  244. {$endif}
  245. {$ifdef USEEXCEPT}
  246. if setjmp(recoverpos)=0 then
  247. begin
  248. recoverpospointer:=@recoverpos;
  249. {$ifdef TP}
  250. do_stop:=recoverstop;
  251. {$else TP}
  252. do_stop:=@recoverstop;
  253. {$endif TP}
  254. {$endif USEEXCEPT}
  255. starttime:=getrealtime;
  256. if parapreprocess then
  257. parser.preprocess(inputdir+inputfile+inputextension)
  258. else
  259. parser.compile(inputdir+inputfile+inputextension,false);
  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. Writeln('Memory Lost = '+tostr(system.HeapSize-MemAvail-EntryMemUsed));
  285. {$endif FPC}
  286. {$ifndef newcg}
  287. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  288. {$endif newcg}
  289. {$endif EXTDEBUG}
  290. {$ifdef fixLeaksOnError}
  291. {$ifdef tp}
  292. do_stop;
  293. {$else tp}
  294. do_stop();
  295. {$endif tp}
  296. {$endif fixLeaksOnError}
  297. end;
  298. end.
  299. {
  300. $Log$
  301. Revision 1.48 2000-04-05 21:18:04 pierre
  302. * set NOUSEEXCEPT to remove use of setjump/longjump
  303. Revision 1.47 2000/03/18 15:05:33 jonas
  304. + added $maxfpuregisters 0 for compile() procedure
  305. Revision 1.46 2000/02/09 13:22:50 peter
  306. * log truncated
  307. Revision 1.45 2000/01/11 17:16:04 jonas
  308. * removed a lot of memory leaks when an error is encountered (caused by
  309. procinfo and pstringcontainers). There are still plenty left though :)
  310. Revision 1.44 2000/01/11 16:56:22 jonas
  311. - removed call to do_stop at the end of compile() since it obviously breaks the
  312. automatic compiling of units. Make cycle worked though! 8)
  313. Revision 1.43 2000/01/11 16:53:24 jonas
  314. + call do_stop at the end of compile()
  315. Revision 1.42 2000/01/07 01:14:23 peter
  316. * updated copyright to 2000
  317. Revision 1.41 1999/12/02 17:34:34 peter
  318. * preprocessor support. But it fails on the caret in type blocks
  319. Revision 1.40 1999/11/18 13:43:48 pierre
  320. + IsExe global var needed for IDE
  321. Revision 1.39 1999/11/12 11:03:50 peter
  322. * searchpaths changed to stringqueue object
  323. Revision 1.38 1999/11/09 23:47:53 pierre
  324. + minimal_stop to avoid memory loss with -iTO switch
  325. Revision 1.37 1999/11/06 14:34:20 peter
  326. * truncated log to 20 revs
  327. Revision 1.36 1999/10/12 21:20:41 florian
  328. * new codegenerator compiles again
  329. Revision 1.35 1999/09/28 19:48:45 florian
  330. * bug 617 fixed
  331. Revision 1.34 1999/09/16 23:05:52 florian
  332. * m68k compiler is again compilable (only gas writer, no assembler reader)
  333. Revision 1.33 1999/09/07 15:10:04 pierre
  334. * use do_halt instead of halt
  335. Revision 1.32 1999/09/02 18:47:44 daniel
  336. * Could not compile with TP, some arrays moved to heap
  337. * NOAG386BIN default for TP
  338. * AG386* files were not compatible with TP, fixed.
  339. Revision 1.31 1999/08/20 10:17:01 michael
  340. + Patch from pierre
  341. Revision 1.30 1999/08/11 17:26:31 peter
  342. * tlinker object is now inherited for win32 and dos
  343. * postprocessexecutable is now a method of tlinker
  344. Revision 1.29 1999/08/09 22:13:43 peter
  345. * fixed writing of lost memory which should be after donecompiler
  346. Revision 1.28 1999/08/04 13:02:40 jonas
  347. * all tokens now start with an underscore
  348. * PowerPC compiles!!
  349. Revision 1.27 1999/08/02 21:28:56 florian
  350. * the main branch psub.pas is now used for
  351. newcg compiler
  352. Revision 1.26 1999/08/02 20:46:57 michael
  353. * Alpha aware switch detection
  354. }