compiler.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. {$ifdef TP}
  61. {$ifdef DPMI}
  62. {$undef USEEXCEPT}
  63. {$endif}
  64. {$endif}
  65. uses
  66. {$ifdef fpc}
  67. {$ifdef GO32V2}
  68. emu387,
  69. dpmiexcp,
  70. {$endif GO32V2}
  71. {$ifdef LINUX}
  72. { catch, }
  73. {$endif LINUX}
  74. {$endif}
  75. {$ifdef USEEXCEPT}
  76. tpexcept,
  77. {$endif USEEXCEPT}
  78. dos,verbose,comphook,systems,
  79. globals,options,parser,symtable,link,import;
  80. function Compile(const cmd:string):longint;
  81. implementation
  82. var
  83. CompilerInited : boolean;
  84. {$ifdef USEEXCEPT}
  85. recoverpos : jmp_buf;
  86. {$endif USEEXCEPT}
  87. {$ifdef USEEXCEPT}
  88. procedure RecoverStop;{$ifndef FPC}far;{$endif}
  89. begin
  90. LongJmp(recoverpos,1);
  91. end;
  92. {$endif USEEXCEPT}
  93. {****************************************************************************
  94. Temp Heap Creation
  95. ****************************************************************************}
  96. {$ifdef TP}
  97. {$ifndef DPMI}
  98. var
  99. oldfreelist,
  100. oldheapptr,
  101. oldheaporg : pointer;
  102. {$endif}
  103. {$endif}
  104. procedure CreateHeap;
  105. begin
  106. {$Ifdef TP}
  107. {$ifndef DPMI}
  108. { Save old heap }
  109. oldfreelist:=freelist;
  110. oldheapptr:=heapptr;
  111. oldheaporg:=heaporg;
  112. { Create a new heap }
  113. heaporg:=oldheapptr;
  114. heapptr:=heaporg;
  115. freelist:=heaporg;
  116. {$endif}
  117. {$endif}
  118. end;
  119. procedure RestoreHeap;
  120. begin
  121. {$Ifdef TP}
  122. {$ifndef DPMI}
  123. { Restore old heap }
  124. freelist:=oldfreelist;
  125. heapptr:=oldheapptr;
  126. heaporg:=oldheaporg;
  127. {$endif}
  128. {$endIf TP}
  129. end;
  130. {****************************************************************************
  131. Compiler
  132. ****************************************************************************}
  133. procedure DoneCompiler;
  134. begin
  135. if not CompilerInited then
  136. exit;
  137. { Free memory }
  138. DoneSymtable;
  139. CompilerInited:=false;
  140. end;
  141. procedure InitCompiler(const cmd:string);
  142. begin
  143. if CompilerInited then
  144. DoneCompiler;
  145. { inits which need to be done before the arguments are parsed }
  146. InitVerbose;
  147. InitGlobals;
  148. InitSymtable;
  149. linker.init;
  150. { read the arguments }
  151. read_arguments(cmd);
  152. { inits which depend on arguments }
  153. initparser;
  154. initimport;
  155. CompilerInited:=true;
  156. end;
  157. function Compile(const cmd:string):longint;
  158. function getrealtime : real;
  159. var
  160. h,m,s,s100 : word;
  161. begin
  162. gettime(h,m,s,s100);
  163. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  164. end;
  165. var
  166. starttime : real;
  167. {$ifdef USEEXCEPT}
  168. olddo_stop : tstopprocedure;
  169. {$endif}
  170. {$IfDef Extdebug}
  171. EntryMemAvail : longint;
  172. {$EndIf}
  173. begin
  174. {$ifdef EXTDEBUG}
  175. EntryMemAvail:=MemAvail;
  176. {$endif}
  177. { Get a new heap }
  178. CreateHeap;
  179. { Initialize the compiler }
  180. InitCompiler(cmd);
  181. { show some info }
  182. Message1(general_u_compilername,FixFileName(paramstr(0)));
  183. Message1(general_d_sourceos,source_os.name);
  184. Message1(general_i_targetos,target_os.name);
  185. Message1(general_u_exepath,exepath);
  186. Message1(general_u_unitpath,unitsearchpath);
  187. Message1(general_u_includepath,includesearchpath);
  188. Message1(general_u_librarypath,Linker.librarysearchpath);
  189. Message1(general_u_objectpath,objectsearchpath);
  190. {$ifdef TP}
  191. Comment(V_Info,'Memory: '+tostr(MemAvail)+' Bytes Free');
  192. {$endif}
  193. {$ifdef USEEXCEPT}
  194. olddo_stop:=do_stop;
  195. do_stop:=recoverstop;
  196. if setjmp(recoverpos)=0 then
  197. begin
  198. {$endif USEEXCEPT}
  199. starttime:=getrealtime;
  200. parser.compile(inputdir+inputfile+inputextension,false);
  201. if status.errorcount=0 then
  202. begin
  203. starttime:=getrealtime-starttime;
  204. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  205. '.'+tostr(trunc(frac(starttime)*10)));
  206. end;
  207. { Stop the compiler, frees also memory }
  208. DoneCompiler;
  209. {$ifdef USEEXCEPT}
  210. end;
  211. { Stop is always called, so we come here when a program is compiled or not }
  212. do_stop:=olddo_stop;
  213. {$endif USEEXCEPT}
  214. {$ifdef EXTDEBUG}
  215. Comment(V_Info,'Memory Lost = '+tostr(EntryMemAvail-MemAvail));
  216. {$endif EXTDEBUG}
  217. { Restore Heap }
  218. RestoreHeap;
  219. { Set the return value if an error has occurred }
  220. if status.errorcount=0 then
  221. Compile:=0
  222. else
  223. Compile:=1;
  224. end;
  225. end.
  226. {
  227. $Log$
  228. Revision 1.6 1998-08-29 13:51:10 peter
  229. * moved get_exepath to globals
  230. + date_string const with the current date for 0.99.7+
  231. Revision 1.5 1998/08/11 21:38:24 peter
  232. + createheap/restoreheap procedures (only tp7 rm currently) and support
  233. for tp7 dpmi
  234. Revision 1.4 1998/08/11 14:09:06 peter
  235. * fixed some messages and smaller msgtxt.inc
  236. Revision 1.3 1998/08/11 00:01:20 peter
  237. * -vu displays now all searchpaths
  238. Revision 1.2 1998/08/10 14:49:56 peter
  239. + localswitches, moduleswitches, globalswitches splitting
  240. Revision 1.1 1998/08/10 10:18:24 peter
  241. + Compiler,Comphook unit which are the new interface units to the
  242. compiler
  243. }