compiler.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. {$ifdef BrowserLog}
  28. browlog,
  29. {$endif BrowserLog}
  30. {$IFNDEF USE_FAKE_SYSUTILS}
  31. sysutils,
  32. {$ELSE}
  33. fksysutl,
  34. {$ENDIF}
  35. verbose,comphook,systems,
  36. cutils,cfileutils,cclasses,globals,options,fmodule,parser,symtable,
  37. assemble,link,dbgbase,import,export,tokens,pass_1
  38. { cpu parameter handling }
  39. ,cpupara
  40. { procinfo stuff }
  41. ,cpupi
  42. { cpu codegenerator }
  43. ,cgcpu
  44. {$ifndef NOPASS2}
  45. ,cpunode
  46. {$endif}
  47. { cpu targets }
  48. ,cputarg
  49. { system information for source system }
  50. { the information about the target os }
  51. { are pulled in by the t_* units }
  52. {$ifdef amiga}
  53. ,i_amiga
  54. {$endif amiga}
  55. {$ifdef atari}
  56. ,i_atari
  57. {$endif atari}
  58. {$ifdef beos}
  59. ,i_beos
  60. {$endif beos}
  61. {$ifdef fbsd}
  62. ,i_fbsd
  63. {$endif fbsd}
  64. {$ifdef gba}
  65. ,i_gba
  66. {$endif gba}
  67. {$ifdef go32v2}
  68. ,i_go32v2
  69. {$endif go32v2}
  70. {$ifdef linux}
  71. ,i_linux
  72. {$endif linux}
  73. {$ifdef macos}
  74. ,i_macos
  75. {$endif macos}
  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. {$ifdef EXTDEBUG}
  107. {$define SHOWUSEDMEM}
  108. {$endif}
  109. {$ifdef 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. {$ifdef BrowserLog}
  124. DoneBrowserLog;
  125. {$endif BrowserLog}
  126. {$ifdef BrowserCol}
  127. do_doneSymbolInfo;
  128. {$endif BrowserCol}
  129. if CompilerInitedAfterArgs then
  130. begin
  131. CompilerInitedAfterArgs:=false;
  132. DoneParser;
  133. DoneImport;
  134. DoneExport;
  135. DoneDebuginfo;
  136. DoneLinker;
  137. DoneAssembler;
  138. DoneAsm;
  139. end;
  140. { Free memory for the others }
  141. CompilerInited:=false;
  142. DoneSymtable;
  143. DoneGlobals;
  144. DoneFileUtils;
  145. donetokens;
  146. end;
  147. procedure InitCompiler(const cmd:string);
  148. begin
  149. if CompilerInited then
  150. DoneCompiler;
  151. { inits which need to be done before the arguments are parsed }
  152. InitSystems;
  153. { fileutils depends on source_info so it must be after systems }
  154. InitFileUtils;
  155. { globals depends on source_info so it must be after systems }
  156. InitGlobals;
  157. { verbose depends on exe_path and must be after globals }
  158. InitVerbose;
  159. {$ifdef BrowserLog}
  160. InitBrowserLog;
  161. {$endif BrowserLog}
  162. {$ifdef BrowserCol}
  163. do_initSymbolInfo;
  164. {$endif BrowserCol}
  165. inittokens;
  166. InitSymtable; {Must come before read_arguments, to enable macrosymstack}
  167. CompilerInited:=true;
  168. { this is needed here for the IDE
  169. in case of compilation failure
  170. at the previous compile }
  171. current_module:=nil;
  172. { read the arguments }
  173. read_arguments(cmd);
  174. { inits which depend on arguments }
  175. InitParser;
  176. InitImport;
  177. InitExport;
  178. InitLinker;
  179. InitAssembler;
  180. InitDebugInfo;
  181. InitAsm;
  182. CompilerInitedAfterArgs:=true;
  183. end;
  184. function Compile(const cmd:string):longint;
  185. {$maxfpuregisters 0}
  186. procedure writepathlist(w:longint;l:TSearchPathList);
  187. var
  188. hp : tstringlistitem;
  189. begin
  190. hp:=tstringlistitem(l.first);
  191. while assigned(hp) do
  192. begin
  193. Message1(w,hp.str);
  194. hp:=tstringlistitem(hp.next);
  195. end;
  196. end;
  197. function getrealtime : real;
  198. var
  199. h,m,s,s1000 : word;
  200. begin
  201. DecodeTime(Time,h,m,s,s1000);
  202. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  203. end;
  204. var
  205. starttime : real;
  206. timestr : string[20];
  207. linkstr : string[64];
  208. {$ifdef SHOWUSEDMEM}
  209. hstatus : TFPCHeapStatus;
  210. {$endif SHOWUSEDMEM}
  211. begin
  212. try
  213. try
  214. { Initialize the compiler }
  215. InitCompiler(cmd);
  216. { show some info }
  217. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  218. Message1(general_d_sourceos,source_info.name);
  219. Message1(general_i_targetos,target_info.name);
  220. Message1(general_t_exepath,exepath);
  221. WritePathList(general_t_unitpath,unitsearchpath);
  222. WritePathList(general_t_includepath,includesearchpath);
  223. WritePathList(general_t_librarypath,librarysearchpath);
  224. WritePathList(general_t_objectpath,objectsearchpath);
  225. starttime:=getrealtime;
  226. { Compile the program }
  227. {$ifdef PREPROCWRITE}
  228. if parapreprocess then
  229. parser.preprocess(inputfilepath+inputfilename)
  230. else
  231. {$endif PREPROCWRITE}
  232. parser.compile(inputfilepath+inputfilename);
  233. { Show statistics }
  234. if status.errorcount=0 then
  235. begin
  236. starttime:=getrealtime-starttime;
  237. if starttime<0 then
  238. starttime:=starttime+3600.0*24.0;
  239. timestr:=tostr(trunc(starttime))+'.'+tostr(trunc(frac(starttime)*10));
  240. if status.codesize<>-1 then
  241. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  242. else
  243. linkstr:='';
  244. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  245. if (Status.Verbosity and V_Warning = V_Warning) and
  246. (Status.CountWarnings <> 0) then
  247. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  248. if (Status.Verbosity and V_Hint = V_Hint) and
  249. (Status.CountHints <> 0) then
  250. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  251. if (Status.Verbosity and V_Note = V_Note) and
  252. (Status.CountNotes <> 0) then
  253. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  254. end;
  255. finally
  256. { no message possible after this !! }
  257. DoneCompiler;
  258. end;
  259. DoneVerbose;
  260. except
  261. on EControlCAbort do
  262. begin
  263. try
  264. { in case of 50 errors, this could cause another exception,
  265. suppress this exception
  266. }
  267. Message(general_f_compilation_aborted);
  268. except
  269. on ECompilerAbort do
  270. ;
  271. end;
  272. DoneVerbose;
  273. end;
  274. on ECompilerAbort do
  275. begin
  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. end;
  287. on ECompilerAbortSilent do
  288. begin
  289. DoneVerbose;
  290. end;
  291. on Exception do
  292. begin
  293. { General catchall, normally not used }
  294. try
  295. { in case of 50 errors, this could cause another exception,
  296. suppress this exception
  297. }
  298. Message(general_f_compilation_aborted);
  299. except
  300. on ECompilerAbort do
  301. ;
  302. end;
  303. DoneVerbose;
  304. Raise;
  305. end;
  306. end;
  307. {$ifdef SHOWUSEDMEM}
  308. hstatus:=GetFPCHeapStatus;
  309. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  310. {$endif SHOWUSEDMEM}
  311. { Set the return value if an error has occurred }
  312. if status.errorcount=0 then
  313. result:=0
  314. else
  315. result:=1;
  316. end;
  317. end.