compiler.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. { 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. implementation
  110. uses
  111. cpubase;
  112. var
  113. CompilerInitedAfterArgs,
  114. CompilerInited : boolean;
  115. olddo_stop : tstopprocedure;
  116. {$ifdef USEEXCEPT}
  117. procedure RecoverStop;{$ifndef FPC}far;{$endif}
  118. begin
  119. if recoverpospointer<>nil then
  120. LongJmp(recoverpospointer^,1)
  121. else
  122. Do_Halt(1);
  123. end;
  124. {$endif USEEXCEPT}
  125. {****************************************************************************
  126. Compiler
  127. ****************************************************************************}
  128. procedure DoneCompiler;
  129. begin
  130. if not CompilerInited then
  131. exit;
  132. { Free compiler if args are read }
  133. {$ifdef BrowserLog}
  134. DoneBrowserLog;
  135. {$endif BrowserLog}
  136. {$ifdef BrowserCol}
  137. DoneBrowserCol;
  138. {$endif BrowserCol}
  139. if CompilerInitedAfterArgs then
  140. begin
  141. CompilerInitedAfterArgs:=false;
  142. doneparser;
  143. DoneImport;
  144. DoneExport;
  145. DoneLinker;
  146. DoneCpu;
  147. end;
  148. { Free memory for the others }
  149. CompilerInited:=false;
  150. DoneSymtable;
  151. DoneGlobals;
  152. donetokens;
  153. {$ifdef USEEXCEPT}
  154. recoverpospointer:=nil;
  155. longjump_used:=false;
  156. {$endif USEEXCEPT}
  157. end;
  158. procedure InitCompiler(const cmd:string);
  159. begin
  160. if CompilerInited then
  161. DoneCompiler;
  162. { inits which need to be done before the arguments are parsed }
  163. InitSystems;
  164. InitVerbose;
  165. {$ifdef BrowserLog}
  166. InitBrowserLog;
  167. {$endif BrowserLog}
  168. {$ifdef BrowserCol}
  169. InitBrowserCol;
  170. {$endif BrowserCol}
  171. InitGlobals;
  172. inittokens;
  173. InitSymtable;
  174. CompilerInited:=true;
  175. { read the arguments }
  176. read_arguments(cmd);
  177. { inits which depend on arguments }
  178. initparser;
  179. InitImport;
  180. InitExport;
  181. InitLinker;
  182. InitCpu;
  183. CompilerInitedAfterArgs:=true;
  184. end;
  185. procedure minimal_stop;{$ifndef fpc}far;{$endif}
  186. begin
  187. DoneCompiler;
  188. olddo_stop;
  189. end;
  190. function Compile(const cmd:string):longint;
  191. procedure writepathlist(w:tmsgconst;l:TSearchPathList);
  192. var
  193. hp : pstringqueueitem;
  194. begin
  195. hp:=l.first;
  196. while assigned(hp) do
  197. begin
  198. Message1(w,hp^.data^);
  199. hp:=hp^.next;
  200. end;
  201. end;
  202. function getrealtime : real;
  203. var
  204. h,m,s,s100 : word;
  205. begin
  206. gettime(h,m,s,s100);
  207. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  208. end;
  209. var
  210. starttime : real;
  211. {$ifdef USEEXCEPT}
  212. recoverpos : jmp_buf;
  213. {$endif}
  214. begin
  215. olddo_stop:=do_stop;
  216. {$ifdef TP}
  217. do_stop:=minimal_stop;
  218. {$else TP}
  219. do_stop:=@minimal_stop;
  220. {$endif TP}
  221. { Initialize the compiler }
  222. InitCompiler(cmd);
  223. { show some info }
  224. Message1(general_t_compilername,FixFileName(paramstr(0)));
  225. Message1(general_d_sourceos,source_os.name);
  226. Message1(general_i_targetos,target_os.name);
  227. Message1(general_t_exepath,exepath);
  228. WritePathList(general_t_unitpath,unitsearchpath);
  229. WritePathList(general_t_includepath,includesearchpath);
  230. WritePathList(general_t_librarypath,librarysearchpath);
  231. WritePathList(general_t_objectpath,objectsearchpath);
  232. {$ifdef TP}
  233. {$ifndef Delphi}
  234. Comment(V_Info,'Memory: '+tostr(MemAvail)+' Bytes Free');
  235. {$endif Delphi}
  236. {$endif}
  237. {$ifdef USEEXCEPT}
  238. if setjmp(recoverpos)=0 then
  239. begin
  240. recoverpospointer:=@recoverpos;
  241. {$ifdef TP}
  242. do_stop:=recoverstop;
  243. {$else TP}
  244. do_stop:=@recoverstop;
  245. {$endif TP}
  246. {$endif USEEXCEPT}
  247. starttime:=getrealtime;
  248. parser.compile(inputdir+inputfile+inputextension,false);
  249. if status.errorcount=0 then
  250. begin
  251. starttime:=getrealtime-starttime;
  252. if starttime<0 then
  253. starttime:=starttime+3600.0*24.0;
  254. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  255. '.'+tostr(trunc(frac(starttime)*10)));
  256. end;
  257. {$ifdef USEEXCEPT}
  258. end;
  259. {$endif USEEXCEPT}
  260. { Stop is always called, so we come here when a program is compiled or not }
  261. do_stop:=olddo_stop;
  262. { Stop the compiler, frees also memory }
  263. { no message possible after this !! }
  264. DoneCompiler;
  265. { Set the return value if an error has occurred }
  266. if status.errorcount=0 then
  267. Compile:=0
  268. else
  269. Compile:=1;
  270. DoneVerbose;
  271. {$ifdef EXTDEBUG}
  272. {$ifdef FPC}
  273. Writeln('Memory Lost = '+tostr(system.HeapSize-MemAvail-EntryMemUsed));
  274. {$endif FPC}
  275. {$ifndef newcg}
  276. Writeln('Repetitive firstpass = '+tostr(firstpass_several)+'/'+tostr(total_of_firstpass));
  277. {$endif newcg}
  278. {$endif EXTDEBUG}
  279. end;
  280. end.
  281. {
  282. $Log$
  283. Revision 1.39 1999-11-12 11:03:50 peter
  284. * searchpaths changed to stringqueue object
  285. Revision 1.38 1999/11/09 23:47:53 pierre
  286. + minimal_stop to avoid memory loss with -iTO switch
  287. Revision 1.37 1999/11/06 14:34:20 peter
  288. * truncated log to 20 revs
  289. Revision 1.36 1999/10/12 21:20:41 florian
  290. * new codegenerator compiles again
  291. Revision 1.35 1999/09/28 19:48:45 florian
  292. * bug 617 fixed
  293. Revision 1.34 1999/09/16 23:05:52 florian
  294. * m68k compiler is again compilable (only gas writer, no assembler reader)
  295. Revision 1.33 1999/09/07 15:10:04 pierre
  296. * use do_halt instead of halt
  297. Revision 1.32 1999/09/02 18:47:44 daniel
  298. * Could not compile with TP, some arrays moved to heap
  299. * NOAG386BIN default for TP
  300. * AG386* files were not compatible with TP, fixed.
  301. Revision 1.31 1999/08/20 10:17:01 michael
  302. + Patch from pierre
  303. Revision 1.30 1999/08/11 17:26:31 peter
  304. * tlinker object is now inherited for win32 and dos
  305. * postprocessexecutable is now a method of tlinker
  306. Revision 1.29 1999/08/09 22:13:43 peter
  307. * fixed writing of lost memory which should be after donecompiler
  308. Revision 1.28 1999/08/04 13:02:40 jonas
  309. * all tokens now start with an underscore
  310. * PowerPC compiles!!
  311. Revision 1.27 1999/08/02 21:28:56 florian
  312. * the main branch psub.pas is now used for
  313. newcg compiler
  314. Revision 1.26 1999/08/02 20:46:57 michael
  315. * Alpha aware switch detection
  316. Revision 1.25 1999/07/18 14:47:22 florian
  317. * bug 487 fixed, (inc(<property>) isn't allowed)
  318. * more fixes to compile with Delphi
  319. Revision 1.24 1999/07/18 10:19:48 florian
  320. * made it compilable with Dlephi 4 again
  321. + fixed problem with large stack allocations on win32
  322. Revision 1.23 1999/06/22 16:24:41 pierre
  323. * local browser stuff corrected
  324. Revision 1.22 1999/05/17 14:24:32 pierre
  325. * DoneCompiler called later to prevent accessing invalid data
  326. Revision 1.21 1999/05/04 21:44:39 florian
  327. * changes to compile it with Delphi 4.0
  328. Revision 1.20 1999/04/21 09:43:33 peter
  329. * storenumber works
  330. * fixed some typos in double_checksum
  331. + incompatible types type1 and type2 message (with storenumber)
  332. Revision 1.19 1999/03/09 11:52:06 pierre
  333. * compilation after a failure longjumped directly to end
  334. Revision 1.18 1999/02/26 00:48:16 peter
  335. * assembler writers fixed for ag386bin
  336. Revision 1.17 1999/01/12 14:25:25 peter
  337. + BrowserLog for browser.log generation
  338. + BrowserCol for browser info in TCollections
  339. * released all other UseBrowser
  340. }