compiler.pas 8.6 KB

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