compiler.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. uses
  61. {$ifdef fpc}
  62. {$ifdef GO32V2}
  63. emu387,
  64. dpmiexcp,
  65. {$endif GO32V2}
  66. {$ifdef LINUX}
  67. catch,
  68. {$endif LINUX}
  69. {$endif}
  70. {$ifdef USEEXCEPT}
  71. tpexcept,
  72. {$endif USEEXCEPT}
  73. dos,verbose,comphook,systems,
  74. globals,options,parser,symtable,link,import;
  75. function Compile(const cmd:string):longint;
  76. implementation
  77. var
  78. CompilerInited : boolean;
  79. recoverpos : jmp_buf;
  80. {$ifdef USEEXCEPT}
  81. procedure RecoverStop;{$ifndef FPC}far;{$endif}
  82. begin
  83. LongJmp(recoverpos,1);
  84. end;
  85. {$endif USEEXCEPT}
  86. procedure DoneCompiler;
  87. begin
  88. if not CompilerInited then
  89. exit;
  90. { Free memory }
  91. DoneSymtable;
  92. CompilerInited:=false;
  93. end;
  94. procedure InitCompiler(const cmd:string);
  95. begin
  96. if CompilerInited then
  97. DoneCompiler;
  98. { inits which need to be done before the arguments are parsed }
  99. get_exepath;
  100. InitVerbose;
  101. InitGlobals;
  102. InitSymtable;
  103. linker.init;
  104. { read the arguments }
  105. read_arguments(cmd);
  106. { inits which depend on arguments }
  107. initparser;
  108. initimport;
  109. CompilerInited:=true;
  110. end;
  111. function Compile(const cmd:string):longint;
  112. function getrealtime : real;
  113. var
  114. h,m,s,s100 : word;
  115. begin
  116. gettime(h,m,s,s100);
  117. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  118. end;
  119. var
  120. starttime : real;
  121. {$ifdef USEEXCEPT}
  122. olddo_stop : tstopprocedure;
  123. {$endif}
  124. {$ifdef TP}
  125. oldfreelist,
  126. oldheapptr,
  127. oldheaporg : pointer;
  128. {$endif}
  129. {$IfDef Extdebug}
  130. EntryMemAvail : longint;
  131. {$EndIf}
  132. begin
  133. {$Ifdef TP}
  134. { Save old heap }
  135. oldfreelist:=freelist;
  136. oldheapptr:=heapptr;
  137. oldheaporg:=heaporg;
  138. { Create a new heap }
  139. heaporg:=oldheapptr;
  140. heapptr:=heaporg;
  141. freelist:=heaporg;
  142. {$endif}
  143. {$ifdef EXTDEBUG}
  144. EntryMemAvail:=MemAvail;
  145. {$endif}
  146. { Initialize the compiler }
  147. InitCompiler(cmd);
  148. { show some info }
  149. Message1(general_i_compilername,FixFileName(paramstr(0)));
  150. Message1(general_i_unitsearchpath,unitsearchpath);
  151. Message1(general_d_sourceos,source_os.name);
  152. Message1(general_i_targetos,target_os.name);
  153. Message1(general_u_exepath,exepath);
  154. Message1(general_u_gcclibpath,Linker.librarysearchpath);
  155. {$ifdef TP}
  156. Comment(V_Info,'Memory: '+tostr(MemAvail)+' Bytes Free');
  157. {$endif}
  158. {$ifdef USEEXCEPT}
  159. olddo_stop:=do_stop;
  160. do_stop:=recoverstop;
  161. if setjmp(recoverpos)=0 then
  162. begin
  163. {$endif USEEXCEPT}
  164. starttime:=getrealtime;
  165. parser.compile(inputdir+inputfile+inputextension,false);
  166. if status.errorcount=0 then
  167. begin
  168. starttime:=getrealtime-starttime;
  169. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  170. '.'+tostr(trunc(frac(starttime)*10)));
  171. end;
  172. { Stop the compiler, frees also memory }
  173. DoneCompiler;
  174. {$ifdef USEEXCEPT}
  175. end;
  176. { Stop is always called, so we come here when a program is compiled or not }
  177. do_stop:=olddo_stop;
  178. {$endif USEEXCEPT}
  179. {$ifdef EXTDEBUG}
  180. Comment(V_Info,'Memory Lost = '+tostr(EntryMemAvail-MemAvail));
  181. {$endif EXTDEBUG}
  182. {$Ifdef TP}
  183. { Restore old heap }
  184. freelist:=oldfreelist;
  185. heapptr:=oldheapptr;
  186. heaporg:=oldheaporg;
  187. {$endIf TP}
  188. { Set the return value if an error has occurred }
  189. if status.errorcount=0 then
  190. Compile:=0
  191. else
  192. Compile:=1;
  193. end;
  194. end.
  195. {
  196. $Log$
  197. Revision 1.2 1998-08-10 14:49:56 peter
  198. + localswitches, moduleswitches, globalswitches splitting
  199. Revision 1.1 1998/08/10 10:18:24 peter
  200. + Compiler,Comphook unit which are the new interface units to the
  201. compiler
  202. }