compiler.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. {
  2. This unit is the interface of the compiler which can be used by
  3. external programs to link in the compiler
  4. Copyright (c) 1998-2005 by Florian Klaempfl
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. unit compiler;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$ifdef GO32V2}
  22. emu387,
  23. {$endif GO32V2}
  24. {$ifdef WATCOM}
  25. emu387,
  26. {$endif WATCOM}
  27. {$IFNDEF USE_FAKE_SYSUTILS}
  28. sysutils,math,
  29. {$ELSE}
  30. fksysutl,
  31. {$ENDIF}
  32. verbose,comphook,systems,
  33. cutils,cfileutl,cclasses,globals,options,fmodule,parser,symtable,
  34. assemble,link,dbgbase,import,export,tokens,pass_1,wpobase,wpo
  35. { cpu parameter handling }
  36. ,cpupara
  37. { procinfo stuff }
  38. ,cpupi
  39. { cpu codegenerator }
  40. ,cgcpu
  41. {$ifndef NOPASS2}
  42. ,cpunode
  43. {$endif}
  44. { cpu targets }
  45. ,cputarg
  46. { system information for source system }
  47. { the information about the target os }
  48. { are pulled in by the t_* units }
  49. {$ifdef amiga}
  50. ,i_amiga
  51. {$endif amiga}
  52. {$ifdef atari}
  53. ,i_atari
  54. {$endif atari}
  55. {$ifdef beos}
  56. ,i_beos
  57. {$endif beos}
  58. {$ifdef fbsd}
  59. ,i_fbsd
  60. {$endif fbsd}
  61. {$ifdef gba}
  62. ,i_gba
  63. {$endif gba}
  64. {$ifdef go32v2}
  65. ,i_go32v2
  66. {$endif go32v2}
  67. {$ifdef linux}
  68. ,i_linux
  69. {$endif linux}
  70. {$ifdef macos}
  71. ,i_macos
  72. {$endif macos}
  73. {$ifdef nds}
  74. ,i_nds
  75. {$endif nds}
  76. {$ifdef nwm}
  77. ,i_nwm
  78. {$endif nwm}
  79. {$ifdef nwl}
  80. ,i_nwl
  81. {$endif nwm}
  82. {$ifdef os2}
  83. {$ifdef emx}
  84. ,i_emx
  85. {$else emx}
  86. ,i_os2
  87. {$endif emx}
  88. {$endif os2}
  89. {$ifdef palmos}
  90. ,i_palmos
  91. {$endif palmos}
  92. {$ifdef solaris}
  93. ,i_sunos
  94. {$endif solaris}
  95. {$ifdef wdosx}
  96. ,i_wdosx
  97. {$endif wdosx}
  98. {$ifdef win32}
  99. ,i_win
  100. {$endif win32}
  101. {$ifdef symbian}
  102. ,i_symbian
  103. {$endif symbian}
  104. ,globtype;
  105. function Compile(const cmd:string):longint;
  106. implementation
  107. uses
  108. aasmcpu;
  109. {$if defined(EXTDEBUG) or defined(MEMDEBUG)}
  110. {$define SHOWUSEDMEM}
  111. {$endif}
  112. var
  113. CompilerInitedAfterArgs,
  114. CompilerInited : boolean;
  115. {****************************************************************************
  116. Compiler
  117. ****************************************************************************}
  118. procedure DoneCompiler;
  119. begin
  120. if not CompilerInited then
  121. exit;
  122. { Free compiler if args are read }
  123. if CompilerInitedAfterArgs then
  124. begin
  125. CompilerInitedAfterArgs:=false;
  126. DoneParser;
  127. DoneImport;
  128. DoneExport;
  129. DoneLinker;
  130. DoneAsm;
  131. DoneWpo;
  132. end;
  133. { Free memory for the others }
  134. CompilerInited:=false;
  135. do_doneSymbolInfo;
  136. DoneSymtable;
  137. DoneGlobals;
  138. DoneFileUtils;
  139. donetokens;
  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. InitSystems;
  147. { fileutils depends on source_info so it must be after systems }
  148. InitFileUtils;
  149. { globals depends on source_info so it must be after systems }
  150. InitGlobals;
  151. { verbose depends on exe_path and must be after globals }
  152. InitVerbose;
  153. inittokens;
  154. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  155. do_initSymbolInfo;
  156. CompilerInited:=true;
  157. { this is needed here for the IDE
  158. in case of compilation failure
  159. at the previous compile }
  160. set_current_module(nil);
  161. { read the arguments }
  162. read_arguments(cmd);
  163. { inits which depend on arguments }
  164. InitParser;
  165. InitImport;
  166. InitExport;
  167. InitLinker;
  168. InitAsm;
  169. InitWpo;
  170. CompilerInitedAfterArgs:=true;
  171. end;
  172. function Compile(const cmd:string):longint;
  173. {$maxfpuregisters 0}
  174. procedure writepathlist(w:longint;l:TSearchPathList);
  175. var
  176. hp : TCmdStrListItem;
  177. begin
  178. hp:=TCmdStrListItem(l.first);
  179. while assigned(hp) do
  180. begin
  181. Message1(w,hp.str);
  182. hp:=TCmdStrListItem(hp.next);
  183. end;
  184. end;
  185. var
  186. timestr : string[20];
  187. linkstr : string[64];
  188. {$ifdef SHOWUSEDMEM}
  189. hstatus : TFPCHeapStatus;
  190. {$endif SHOWUSEDMEM}
  191. ExceptionMask : TFPUExceptionMask;
  192. totaltime : real;
  193. begin
  194. try
  195. try
  196. ExceptionMask:=GetExceptionMask;
  197. SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
  198. exOverflow, exUnderflow, exPrecision]);
  199. starttime:=getrealtime;
  200. { Initialize the compiler }
  201. InitCompiler(cmd);
  202. { show some info }
  203. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  204. Message1(general_d_sourceos,source_info.name);
  205. Message1(general_i_targetos,target_info.name);
  206. Message1(general_t_exepath,exepath);
  207. WritePathList(general_t_unitpath,unitsearchpath);
  208. WritePathList(general_t_includepath,includesearchpath);
  209. WritePathList(general_t_librarypath,librarysearchpath);
  210. WritePathList(general_t_objectpath,objectsearchpath);
  211. { Compile the program }
  212. {$ifdef PREPROCWRITE}
  213. if parapreprocess then
  214. parser.preprocess(inputfilepath+inputfilename)
  215. else
  216. {$endif PREPROCWRITE}
  217. parser.compile(inputfilepath+inputfilename);
  218. { Show statistics }
  219. if status.errorcount=0 then
  220. begin
  221. totaltime:=getrealtime-starttime;
  222. if totaltime<0 then
  223. totaltime:=totaltime+3600.0*24.0;
  224. timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
  225. if status.codesize<>aword(-1) then
  226. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  227. else
  228. linkstr:='';
  229. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  230. if (Status.Verbosity and V_Warning = V_Warning) and
  231. (Status.CountWarnings <> 0) then
  232. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  233. if (Status.Verbosity and V_Hint = V_Hint) and
  234. (Status.CountHints <> 0) then
  235. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  236. if (Status.Verbosity and V_Note = V_Note) and
  237. (Status.CountNotes <> 0) then
  238. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  239. end;
  240. finally
  241. { no message possible after this !! }
  242. DoneCompiler;
  243. SetExceptionMask(ExceptionMask);
  244. end;
  245. DoneVerbose;
  246. except
  247. on EControlCAbort do
  248. begin
  249. try
  250. { in case of 50 errors, this could cause another exception,
  251. suppress this exception
  252. }
  253. Message(general_f_compilation_aborted);
  254. except
  255. on ECompilerAbort do
  256. ;
  257. end;
  258. DoneVerbose;
  259. end;
  260. on ECompilerAbort do
  261. begin
  262. try
  263. { in case of 50 errors, this could cause another exception,
  264. suppress this exception
  265. }
  266. Message(general_f_compilation_aborted);
  267. except
  268. on ECompilerAbort do
  269. ;
  270. end;
  271. DoneVerbose;
  272. end;
  273. on ECompilerAbortSilent do
  274. begin
  275. DoneVerbose;
  276. end;
  277. on Exception do
  278. begin
  279. { General catchall, normally not used }
  280. try
  281. { in case of 50 errors, this could cause another exception,
  282. suppress this exception
  283. }
  284. Message(general_f_compilation_aborted);
  285. except
  286. on ECompilerAbort do
  287. ;
  288. end;
  289. DoneVerbose;
  290. Raise;
  291. end;
  292. end;
  293. {$ifdef SHOWUSEDMEM}
  294. hstatus:=GetFPCHeapStatus;
  295. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  296. {$endif SHOWUSEDMEM}
  297. { Set the return value if an error has occurred }
  298. if status.errorcount=0 then
  299. result:=0
  300. else
  301. result:=1;
  302. end;
  303. end.