compiler.pas 9.9 KB

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