2
0

compiler.pas 10 KB

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