compiler.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. { some units are implicitly needed by the compiler }
  20. {$WARN 5023 off : Unit "$1" not used in $2}
  21. interface
  22. uses
  23. {$ifdef GO32V2}
  24. emu387,
  25. {$endif GO32V2}
  26. {$ifdef WATCOM}
  27. emu387,
  28. {$endif WATCOM}
  29. {$if defined(unix) and (FPC_FULLVERSION>20700)}
  30. { system code page stuff for unix }
  31. unixcp,
  32. { we also need unit fpwidestring for the codepage-aware messages support, but
  33. unfortunately, pulling this unit in, causes regressions in the following
  34. tests:
  35. test/packages/bzip2/tbzip2streamtest
  36. test/units/system/tres
  37. test/units/system/tres2
  38. test/units/system/tres3
  39. test/units/system/tres4
  40. this needs to be resolved first, before this unit is included. }
  41. // fpwidestring,
  42. {$endif}
  43. {$IFNDEF USE_FAKE_SYSUTILS}
  44. sysutils,math,
  45. {$ELSE}
  46. fksysutl,
  47. {$ENDIF}
  48. verbose,comphook,systems,
  49. cutils,cfileutl,cclasses,globals,options,fmodule,parser,symtable,
  50. assemble,link,dbgbase,import,export,tokens,wpo
  51. { cpu parameter handling }
  52. ,cpupara
  53. { procinfo stuff }
  54. ,cpupi
  55. { cpu codegenerator }
  56. ,cgcpu
  57. {$ifndef NOPASS2}
  58. ,cpunode
  59. {$endif}
  60. { cpu targets }
  61. ,cputarg
  62. {$ifdef llvm}
  63. ,llvmtarg
  64. {$endif llvm}
  65. { system information for source system }
  66. { the information about the target os }
  67. { are pulled in by the t_* units }
  68. {$ifdef amiga}
  69. ,i_amiga
  70. {$endif amiga}
  71. {$ifdef android}
  72. ,i_android
  73. {$endif android}
  74. {$ifdef aros}
  75. ,i_aros
  76. {$endif}
  77. {$ifdef atari}
  78. ,i_atari
  79. {$endif atari}
  80. {$ifdef beos}
  81. ,i_beos
  82. {$endif beos}
  83. {$ifdef fbsd}
  84. ,i_fbsd
  85. {$endif fbsd}
  86. {$ifdef gba}
  87. ,i_gba
  88. {$endif gba}
  89. {$ifdef go32v2}
  90. ,i_go32v2
  91. {$endif go32v2}
  92. {$ifdef linux}
  93. ,i_linux
  94. {$endif linux}
  95. {$ifdef macos}
  96. ,i_macos
  97. {$endif macos}
  98. {$ifdef nds}
  99. ,i_nds
  100. {$endif nds}
  101. {$ifdef nwm}
  102. ,i_nwm
  103. {$endif nwm}
  104. {$ifdef nwl}
  105. ,i_nwl
  106. {$endif nwm}
  107. {$ifdef os2}
  108. {$ifdef emx}
  109. ,i_emx
  110. {$else emx}
  111. ,i_os2
  112. {$endif emx}
  113. {$endif os2}
  114. {$ifdef palmos}
  115. ,i_palmos
  116. {$endif palmos}
  117. {$ifdef solaris}
  118. ,i_sunos
  119. {$endif solaris}
  120. {$ifdef wdosx}
  121. ,i_wdosx
  122. {$endif wdosx}
  123. {$ifdef wii}
  124. ,i_wii
  125. {$endif wii}
  126. {$ifdef win32}
  127. ,i_win
  128. {$endif win32}
  129. {$ifdef symbian}
  130. ,i_symbian
  131. {$endif symbian}
  132. {$ifdef nativent}
  133. ,i_nativent
  134. {$endif nativent}
  135. {$ifdef aix}
  136. ,i_aix
  137. {$endif aix}
  138. ,globtype;
  139. function Compile(const cmd:TCmdStr):longint;
  140. implementation
  141. uses
  142. aasmcpu;
  143. {$if defined(MEMDEBUG)}
  144. {$define SHOWUSEDMEM}
  145. {$endif}
  146. var
  147. CompilerInitedAfterArgs,
  148. CompilerInited : boolean;
  149. {****************************************************************************
  150. Compiler
  151. ****************************************************************************}
  152. procedure DoneCompiler;
  153. begin
  154. if not CompilerInited then
  155. exit;
  156. { Free compiler if args are read }
  157. if CompilerInitedAfterArgs then
  158. begin
  159. CompilerInitedAfterArgs:=false;
  160. DoneParser;
  161. DoneImport;
  162. DoneExport;
  163. DoneLinker;
  164. DoneAsm;
  165. DoneWpo;
  166. end;
  167. { Free memory for the others }
  168. CompilerInited:=false;
  169. do_doneSymbolInfo;
  170. DoneSymtable;
  171. DoneGlobals;
  172. DoneFileUtils;
  173. donetokens;
  174. end;
  175. procedure InitCompiler(const cmd:TCmdStr);
  176. begin
  177. if CompilerInited then
  178. DoneCompiler;
  179. {$if defined(unix) and (FPC_FULLVERSION>20700)}
  180. { Set default code page for ansistrings on unix-like systems }
  181. DefaultSystemCodePage:=GetSystemCodePage;
  182. {$endif}
  183. { inits which need to be done before the arguments are parsed }
  184. InitSystems;
  185. { fileutils depends on source_info so it must be after systems }
  186. InitFileUtils;
  187. { globals depends on source_info so it must be after systems }
  188. InitGlobals;
  189. { verbose depends on exe_path and must be after globals }
  190. InitVerbose;
  191. inittokens;
  192. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  193. do_initSymbolInfo;
  194. CompilerInited:=true;
  195. { this is needed here for the IDE
  196. in case of compilation failure
  197. at the previous compile }
  198. set_current_module(nil);
  199. { read the arguments }
  200. read_arguments(cmd);
  201. { inits which depend on arguments }
  202. InitParser;
  203. InitImport;
  204. InitExport;
  205. InitLinker;
  206. InitAsm;
  207. InitWpo;
  208. CompilerInitedAfterArgs:=true;
  209. end;
  210. function Compile(const cmd:TCmdStr):longint;
  211. {$maxfpuregisters 0}
  212. procedure writepathlist(w:longint;l:TSearchPathList);
  213. var
  214. hp : TCmdStrListItem;
  215. begin
  216. hp:=TCmdStrListItem(l.first);
  217. while assigned(hp) do
  218. begin
  219. Message1(w,hp.str);
  220. hp:=TCmdStrListItem(hp.next);
  221. end;
  222. end;
  223. var
  224. timestr : string[20];
  225. linkstr : string[64];
  226. {$ifdef SHOWUSEDMEM}
  227. hstatus : TFPCHeapStatus;
  228. {$endif SHOWUSEDMEM}
  229. ExceptionMask : TFPUExceptionMask;
  230. totaltime : real;
  231. begin
  232. try
  233. try
  234. ExceptionMask:=GetExceptionMask;
  235. SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
  236. exOverflow, exUnderflow, exPrecision]);
  237. starttime:=getrealtime;
  238. { Initialize the compiler }
  239. InitCompiler(cmd);
  240. { show some info }
  241. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  242. Message1(general_d_sourceos,source_info.name);
  243. Message1(general_i_targetos,target_info.name);
  244. Message1(general_t_exepath,exepath);
  245. WritePathList(general_t_unitpath,unitsearchpath);
  246. WritePathList(general_t_includepath,includesearchpath);
  247. WritePathList(general_t_librarypath,librarysearchpath);
  248. WritePathList(general_t_objectpath,objectsearchpath);
  249. { Compile the program }
  250. {$ifdef PREPROCWRITE}
  251. if parapreprocess then
  252. parser.preprocess(inputfilepath+inputfilename)
  253. else
  254. {$endif PREPROCWRITE}
  255. parser.compile(inputfilepath+inputfilename);
  256. { Show statistics }
  257. if status.errorcount=0 then
  258. begin
  259. totaltime:=getrealtime-starttime;
  260. if totaltime<0 then
  261. totaltime:=totaltime+3600.0*24.0;
  262. if round(frac(totaltime)*10) >= 10 then
  263. totaltime:=trunc(totaltime) + 1;
  264. timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
  265. if status.codesize<>aword(-1) then
  266. linkstr:=', '+tostr(status.codesize)+' ' +MessageStr(general_text_bytes_code)+', '+tostr(status.datasize)+' '+MessageStr(general_text_bytes_data)
  267. else
  268. linkstr:='';
  269. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  270. if (Status.Verbosity and V_Warning = V_Warning) and
  271. (Status.CountWarnings <> 0) then
  272. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  273. if (Status.Verbosity and V_Hint = V_Hint) and
  274. (Status.CountHints <> 0) then
  275. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  276. if (Status.Verbosity and V_Note = V_Note) and
  277. (Status.CountNotes <> 0) then
  278. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  279. end;
  280. finally
  281. { no message possible after this !! }
  282. DoneCompiler;
  283. SetExceptionMask(ExceptionMask);
  284. end;
  285. DoneVerbose;
  286. except
  287. on EControlCAbort do
  288. begin
  289. try
  290. { in case of 50 errors, this could cause another exception,
  291. suppress this exception
  292. }
  293. Message(general_f_compilation_aborted);
  294. except
  295. on ECompilerAbort do
  296. ;
  297. end;
  298. DoneVerbose;
  299. end;
  300. on ECompilerAbort do
  301. begin
  302. try
  303. { in case of 50 errors, this could cause another exception,
  304. suppress this exception
  305. }
  306. Message(general_f_compilation_aborted);
  307. except
  308. on ECompilerAbort do
  309. ;
  310. end;
  311. DoneVerbose;
  312. end;
  313. on ECompilerAbortSilent do
  314. begin
  315. DoneVerbose;
  316. end;
  317. on EOutOfMemory do
  318. begin
  319. try
  320. Message(general_f_no_memory_left);
  321. except
  322. on ECompilerAbort do
  323. ;
  324. end;
  325. DoneVerbose;
  326. end;
  327. on e : EInOutError do
  328. begin
  329. try
  330. Message1(general_f_ioerror,e.message);
  331. except
  332. on ECompilerAbort do
  333. ;
  334. end;
  335. DoneVerbose;
  336. end;
  337. on e : EOSError do
  338. begin
  339. try
  340. Message1(general_f_oserror,e.message);
  341. except
  342. on ECompilerAbort do
  343. ;
  344. end;
  345. DoneVerbose;
  346. end;
  347. on Exception do
  348. begin
  349. { General catchall, normally not used }
  350. try
  351. { in case of 50 errors, this could cause another exception,
  352. suppress this exception
  353. }
  354. if not exception_raised then
  355. begin
  356. exception_raised:=true;
  357. Message(general_e_exception_raised);
  358. end
  359. else
  360. Message(general_f_compilation_aborted);
  361. except
  362. on ECompilerAbort do
  363. ;
  364. end;
  365. DoneVerbose;
  366. Raise;
  367. end;
  368. end;
  369. {$ifdef SHOWUSEDMEM}
  370. hstatus:=GetFPCHeapStatus;
  371. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  372. {$endif SHOWUSEDMEM}
  373. { Set the return value if an error has occurred }
  374. if status.errorcount=0 then
  375. result:=0
  376. else
  377. result:=1;
  378. end;
  379. end.