compiler.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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,
  29. {$ELSE}
  30. fksysutl,
  31. {$ENDIF}
  32. verbose,comphook,systems,
  33. cutils,cfileutils,cclasses,globals,options,fmodule,parser,symtable,
  34. assemble,link,dbgbase,import,export,tokens,pass_1
  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 nwm}
  74. ,i_nwm
  75. {$endif nwm}
  76. {$ifdef nwl}
  77. ,i_nwl
  78. {$endif nwm}
  79. {$ifdef os2}
  80. {$ifdef emx}
  81. ,i_emx
  82. {$else emx}
  83. ,i_os2
  84. {$endif emx}
  85. {$endif os2}
  86. {$ifdef palmos}
  87. ,i_palmos
  88. {$endif palmos}
  89. {$ifdef solaris}
  90. ,i_sunos
  91. {$endif solaris}
  92. {$ifdef wdosx}
  93. ,i_wdosx
  94. {$endif wdosx}
  95. {$ifdef win32}
  96. ,i_win
  97. {$endif win32}
  98. ;
  99. function Compile(const cmd:string):longint;
  100. implementation
  101. uses
  102. aasmcpu;
  103. {$if defined(EXTDEBUG) or defined(MEMDEBUG)}
  104. {$define SHOWUSEDMEM}
  105. {$endif}
  106. var
  107. CompilerInitedAfterArgs,
  108. CompilerInited : boolean;
  109. {****************************************************************************
  110. Compiler
  111. ****************************************************************************}
  112. procedure DoneCompiler;
  113. begin
  114. if not CompilerInited then
  115. exit;
  116. { Free compiler if args are read }
  117. if CompilerInitedAfterArgs then
  118. begin
  119. CompilerInitedAfterArgs:=false;
  120. DoneParser;
  121. DoneImport;
  122. DoneExport;
  123. DoneDebuginfo;
  124. DoneLinker;
  125. DoneAssembler;
  126. DoneAsm;
  127. end;
  128. { Free memory for the others }
  129. CompilerInited:=false;
  130. DoneSymtable;
  131. DoneGlobals;
  132. DoneFileUtils;
  133. donetokens;
  134. end;
  135. procedure InitCompiler(const cmd:string);
  136. begin
  137. if CompilerInited then
  138. DoneCompiler;
  139. { inits which need to be done before the arguments are parsed }
  140. InitSystems;
  141. { fileutils depends on source_info so it must be after systems }
  142. InitFileUtils;
  143. { globals depends on source_info so it must be after systems }
  144. InitGlobals;
  145. { verbose depends on exe_path and must be after globals }
  146. InitVerbose;
  147. inittokens;
  148. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  149. CompilerInited:=true;
  150. { this is needed here for the IDE
  151. in case of compilation failure
  152. at the previous compile }
  153. current_module:=nil;
  154. { read the arguments }
  155. read_arguments(cmd);
  156. { inits which depend on arguments }
  157. InitParser;
  158. InitImport;
  159. InitExport;
  160. InitLinker;
  161. InitAssembler;
  162. InitDebugInfo;
  163. InitAsm;
  164. CompilerInitedAfterArgs:=true;
  165. end;
  166. function Compile(const cmd:string):longint;
  167. {$maxfpuregisters 0}
  168. procedure writepathlist(w:longint;l:TSearchPathList);
  169. var
  170. hp : tstringlistitem;
  171. begin
  172. hp:=tstringlistitem(l.first);
  173. while assigned(hp) do
  174. begin
  175. Message1(w,hp.str);
  176. hp:=tstringlistitem(hp.next);
  177. end;
  178. end;
  179. function getrealtime : real;
  180. var
  181. h,m,s,s1000 : word;
  182. begin
  183. DecodeTime(Time,h,m,s,s1000);
  184. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  185. end;
  186. var
  187. starttime : real;
  188. timestr : string[20];
  189. linkstr : string[64];
  190. {$ifdef SHOWUSEDMEM}
  191. hstatus : TFPCHeapStatus;
  192. {$endif SHOWUSEDMEM}
  193. begin
  194. try
  195. try
  196. { Initialize the compiler }
  197. InitCompiler(cmd);
  198. { show some info }
  199. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  200. Message1(general_d_sourceos,source_info.name);
  201. Message1(general_i_targetos,target_info.name);
  202. Message1(general_t_exepath,exepath);
  203. WritePathList(general_t_unitpath,unitsearchpath);
  204. WritePathList(general_t_includepath,includesearchpath);
  205. WritePathList(general_t_librarypath,librarysearchpath);
  206. WritePathList(general_t_objectpath,objectsearchpath);
  207. starttime:=getrealtime;
  208. { Compile the program }
  209. {$ifdef PREPROCWRITE}
  210. if parapreprocess then
  211. parser.preprocess(inputfilepath+inputfilename)
  212. else
  213. {$endif PREPROCWRITE}
  214. parser.compile(inputfilepath+inputfilename);
  215. { Show statistics }
  216. if status.errorcount=0 then
  217. begin
  218. starttime:=getrealtime-starttime;
  219. if starttime<0 then
  220. starttime:=starttime+3600.0*24.0;
  221. timestr:=tostr(trunc(starttime))+'.'+tostr(trunc(frac(starttime)*10));
  222. if status.codesize<>-1 then
  223. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  224. else
  225. linkstr:='';
  226. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  227. if (Status.Verbosity and V_Warning = V_Warning) and
  228. (Status.CountWarnings <> 0) then
  229. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  230. if (Status.Verbosity and V_Hint = V_Hint) and
  231. (Status.CountHints <> 0) then
  232. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  233. if (Status.Verbosity and V_Note = V_Note) and
  234. (Status.CountNotes <> 0) then
  235. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  236. end;
  237. finally
  238. { no message possible after this !! }
  239. DoneCompiler;
  240. end;
  241. DoneVerbose;
  242. except
  243. on EControlCAbort do
  244. begin
  245. try
  246. { in case of 50 errors, this could cause another exception,
  247. suppress this exception
  248. }
  249. Message(general_f_compilation_aborted);
  250. except
  251. on ECompilerAbort do
  252. ;
  253. end;
  254. DoneVerbose;
  255. end;
  256. on ECompilerAbort do
  257. begin
  258. try
  259. { in case of 50 errors, this could cause another exception,
  260. suppress this exception
  261. }
  262. Message(general_f_compilation_aborted);
  263. except
  264. on ECompilerAbort do
  265. ;
  266. end;
  267. DoneVerbose;
  268. end;
  269. on ECompilerAbortSilent do
  270. begin
  271. DoneVerbose;
  272. end;
  273. on Exception do
  274. begin
  275. { General catchall, normally not used }
  276. try
  277. { in case of 50 errors, this could cause another exception,
  278. suppress this exception
  279. }
  280. Message(general_f_compilation_aborted);
  281. except
  282. on ECompilerAbort do
  283. ;
  284. end;
  285. DoneVerbose;
  286. Raise;
  287. end;
  288. end;
  289. {$ifdef SHOWUSEDMEM}
  290. hstatus:=GetFPCHeapStatus;
  291. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  292. {$endif SHOWUSEDMEM}
  293. { Set the return value if an error has occurred }
  294. if status.errorcount=0 then
  295. result:=0
  296. else
  297. result:=1;
  298. end;
  299. end.