compiler.pas 5.4 KB

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