compiler.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 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. ;
  102. function Compile(const cmd:string):longint;
  103. implementation
  104. uses
  105. aasmcpu;
  106. {$if defined(EXTDEBUG) or defined(MEMDEBUG)}
  107. {$define SHOWUSEDMEM}
  108. {$endif}
  109. var
  110. CompilerInitedAfterArgs,
  111. CompilerInited : boolean;
  112. {****************************************************************************
  113. Compiler
  114. ****************************************************************************}
  115. procedure DoneCompiler;
  116. begin
  117. if not CompilerInited then
  118. exit;
  119. { Free compiler if args are read }
  120. if CompilerInitedAfterArgs then
  121. begin
  122. CompilerInitedAfterArgs:=false;
  123. DoneParser;
  124. DoneImport;
  125. DoneExport;
  126. DoneDebuginfo;
  127. DoneLinker;
  128. DoneAssembler;
  129. DoneAsm;
  130. end;
  131. { Free memory for the others }
  132. CompilerInited:=false;
  133. DoneSymtable;
  134. DoneGlobals;
  135. DoneFileUtils;
  136. donetokens;
  137. end;
  138. procedure InitCompiler(const cmd:string);
  139. begin
  140. if CompilerInited then
  141. DoneCompiler;
  142. { inits which need to be done before the arguments are parsed }
  143. InitSystems;
  144. { fileutils depends on source_info so it must be after systems }
  145. InitFileUtils;
  146. { globals depends on source_info so it must be after systems }
  147. InitGlobals;
  148. { verbose depends on exe_path and must be after globals }
  149. InitVerbose;
  150. inittokens;
  151. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  152. CompilerInited:=true;
  153. { this is needed here for the IDE
  154. in case of compilation failure
  155. at the previous compile }
  156. current_module:=nil;
  157. { read the arguments }
  158. read_arguments(cmd);
  159. { inits which depend on arguments }
  160. InitParser;
  161. InitImport;
  162. InitExport;
  163. InitLinker;
  164. InitAssembler;
  165. InitDebugInfo;
  166. InitAsm;
  167. CompilerInitedAfterArgs:=true;
  168. end;
  169. function Compile(const cmd:string):longint;
  170. {$maxfpuregisters 0}
  171. procedure writepathlist(w:longint;l:TSearchPathList);
  172. var
  173. hp : tstringlistitem;
  174. begin
  175. hp:=tstringlistitem(l.first);
  176. while assigned(hp) do
  177. begin
  178. Message1(w,hp.str);
  179. hp:=tstringlistitem(hp.next);
  180. end;
  181. end;
  182. function getrealtime : real;
  183. var
  184. h,m,s,s1000 : word;
  185. begin
  186. DecodeTime(Time,h,m,s,s1000);
  187. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  188. end;
  189. var
  190. starttime : real;
  191. timestr : string[20];
  192. linkstr : string[64];
  193. {$ifdef SHOWUSEDMEM}
  194. hstatus : TFPCHeapStatus;
  195. {$endif SHOWUSEDMEM}
  196. begin
  197. try
  198. try
  199. { Initialize the compiler }
  200. InitCompiler(cmd);
  201. { show some info }
  202. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  203. Message1(general_d_sourceos,source_info.name);
  204. Message1(general_i_targetos,target_info.name);
  205. Message1(general_t_exepath,exepath);
  206. WritePathList(general_t_unitpath,unitsearchpath);
  207. WritePathList(general_t_includepath,includesearchpath);
  208. WritePathList(general_t_librarypath,librarysearchpath);
  209. WritePathList(general_t_objectpath,objectsearchpath);
  210. starttime:=getrealtime;
  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. starttime:=getrealtime-starttime;
  222. if starttime<0 then
  223. starttime:=starttime+3600.0*24.0;
  224. timestr:=tostr(trunc(starttime))+'.'+tostr(trunc(frac(starttime)*10));
  225. if status.codesize<>-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. end;
  244. DoneVerbose;
  245. except
  246. on EControlCAbort do
  247. begin
  248. try
  249. { in case of 50 errors, this could cause another exception,
  250. suppress this exception
  251. }
  252. Message(general_f_compilation_aborted);
  253. except
  254. on ECompilerAbort do
  255. ;
  256. end;
  257. DoneVerbose;
  258. end;
  259. on ECompilerAbort do
  260. begin
  261. try
  262. { in case of 50 errors, this could cause another exception,
  263. suppress this exception
  264. }
  265. Message(general_f_compilation_aborted);
  266. except
  267. on ECompilerAbort do
  268. ;
  269. end;
  270. DoneVerbose;
  271. end;
  272. on ECompilerAbortSilent do
  273. begin
  274. DoneVerbose;
  275. end;
  276. on Exception do
  277. begin
  278. { General catchall, normally not used }
  279. try
  280. { in case of 50 errors, this could cause another exception,
  281. suppress this exception
  282. }
  283. Message(general_f_compilation_aborted);
  284. except
  285. on ECompilerAbort do
  286. ;
  287. end;
  288. DoneVerbose;
  289. Raise;
  290. end;
  291. end;
  292. {$ifdef SHOWUSEDMEM}
  293. hstatus:=GetFPCHeapStatus;
  294. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  295. {$endif SHOWUSEDMEM}
  296. { Set the return value if an error has occurred }
  297. if status.errorcount=0 then
  298. result:=0
  299. else
  300. result:=1;
  301. end;
  302. end.