compiler.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 human68k}
  91. ,i_human68k
  92. {$endif human68k}
  93. {$ifdef linux}
  94. ,i_linux
  95. {$endif linux}
  96. {$ifdef macos}
  97. ,i_macos
  98. {$endif macos}
  99. {$ifdef morphos}
  100. ,i_morph
  101. {$endif morphos}
  102. {$ifdef nds}
  103. ,i_nds
  104. {$endif nds}
  105. {$ifdef nwm}
  106. ,i_nwm
  107. {$endif nwm}
  108. {$ifdef nwl}
  109. ,i_nwl
  110. {$endif nwm}
  111. {$ifdef os2}
  112. {$ifdef emx}
  113. ,i_emx
  114. {$else emx}
  115. ,i_os2
  116. {$endif emx}
  117. {$endif os2}
  118. {$ifdef palmos}
  119. ,i_palmos
  120. {$endif palmos}
  121. {$ifdef solaris}
  122. ,i_sunos
  123. {$endif solaris}
  124. {$ifdef sinclairql}
  125. ,i_sinclairql
  126. {$endif sinclairql}
  127. {$ifdef wdosx}
  128. ,i_wdosx
  129. {$endif wdosx}
  130. {$ifdef wii}
  131. ,i_wii
  132. {$endif wii}
  133. {$ifdef windows}
  134. ,i_win
  135. {$endif windows}
  136. {$ifdef symbian}
  137. ,i_symbian
  138. {$endif symbian}
  139. {$ifdef nativent}
  140. ,i_nativent
  141. {$endif nativent}
  142. {$ifdef aix}
  143. ,i_aix
  144. {$endif aix}
  145. ,ctask
  146. ,globtype;
  147. function Compile(const cmd:TCmdStr):longint;
  148. implementation
  149. uses
  150. finput,
  151. fppu,
  152. aasmcpu;
  153. {$if defined(MEMDEBUG)}
  154. {$define SHOWUSEDMEM}
  155. {$endif}
  156. var
  157. CompilerInitedAfterArgs,
  158. CompilerInited : boolean;
  159. {****************************************************************************
  160. Compiler
  161. ****************************************************************************}
  162. procedure DoneCompiler;
  163. begin
  164. if not CompilerInited then
  165. exit;
  166. { Free compiler if args are read }
  167. if CompilerInitedAfterArgs then
  168. begin
  169. CompilerInitedAfterArgs:=false;
  170. DoneParser;
  171. DoneImport;
  172. DoneExport;
  173. DoneLinker;
  174. DoneAsm;
  175. DoneWpo;
  176. end;
  177. { Free memory for the others }
  178. CompilerInited:=false;
  179. do_doneSymbolInfo;
  180. DoneSymtable;
  181. DoneGlobals;
  182. DoneFileUtils;
  183. donetokens;
  184. DoneTaskHandler;
  185. end;
  186. procedure InitCompiler(const cmd:TCmdStr);
  187. begin
  188. if CompilerInited then
  189. DoneCompiler;
  190. {$if defined(unix)}
  191. { Set default code page for ansistrings on unix-like systems }
  192. DefaultSystemCodePage:=GetSystemCodePage;
  193. {$endif}
  194. { inits which need to be done before the arguments are parsed }
  195. InitSystems;
  196. { fileutils depends on source_info so it must be after systems }
  197. InitFileUtils;
  198. { globals depends on source_info so it must be after systems }
  199. InitGlobals;
  200. { verbose depends on exe_path and must be after globals }
  201. InitVerbose;
  202. inittokens;
  203. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  204. do_initSymbolInfo;
  205. CompilerInited:=true;
  206. { this is needed here for the IDE
  207. in case of compilation failure
  208. at the previous compile }
  209. set_current_module(nil);
  210. { read the arguments }
  211. read_arguments(cmd);
  212. { inits which depend on arguments }
  213. InitParser;
  214. InitImport;
  215. InitExport;
  216. InitLinker;
  217. InitAsm;
  218. InitWpo;
  219. InitTaskHandler;
  220. CompilerInitedAfterArgs:=true;
  221. end;
  222. function Compile(const cmd:TCmdStr):longint;
  223. {$maxfpuregisters 0}
  224. procedure writecmdstrlist(w:longint;l:TCmdStrList);
  225. var
  226. hp : TCmdStrListItem;
  227. begin
  228. hp:=TCmdStrListItem(l.first);
  229. while assigned(hp) do
  230. begin
  231. Message1(w,hp.str);
  232. hp:=TCmdStrListItem(hp.next);
  233. end;
  234. end;
  235. var
  236. timestr : string[20];
  237. linkstr : string[64];
  238. {$ifdef SHOWUSEDMEM}
  239. hstatus : TFPCHeapStatus;
  240. {$endif SHOWUSEDMEM}
  241. ExceptionMask : TFPUExceptionMask;
  242. totaltime : real;
  243. m : tppumodule;
  244. begin
  245. m:=nil;
  246. try
  247. try
  248. ExceptionMask:=GetExceptionMask;
  249. SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
  250. exOverflow, exUnderflow, exPrecision]);
  251. GetLocalTime(startsystime);
  252. starttime := getrealtime(startsystime);
  253. { Initialize the compiler }
  254. InitCompiler(cmd);
  255. { show some info }
  256. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  257. Message1(general_d_sourceos,source_info.name);
  258. Message1(general_i_targetos,target_info.name);
  259. Message1(general_t_exepath,exepath);
  260. WriteCmdStrList(general_t_unitpath,unitsearchpath);
  261. WriteCmdStrList(general_t_includepath,includesearchpath);
  262. WriteCmdStrList(general_t_librarypath,librarysearchpath);
  263. WriteCmdStrList(general_t_objectpath,objectsearchpath);
  264. WriteCmdStrList(general_t_unitscope,namespacelist);
  265. { Compile the program }
  266. {$ifdef PREPROCWRITE}
  267. if parapreprocess then
  268. parser.preprocess(inputfilepath+inputfilename)
  269. else
  270. {$endif PREPROCWRITE}
  271. begin
  272. m:=tppumodule.create(Nil,'',inputfilepath+inputfilename,false);
  273. m.state:=ms_compile;
  274. m.is_initial:=true;
  275. { We need to add the initial module manually to the list of units }
  276. addloadedunit(m);
  277. main_module:=m;
  278. m.state:=ms_compile;
  279. task_handler.addmodule(m);
  280. task_handler.processqueue;
  281. end;
  282. { Show statistics }
  283. if status.errorcount=0 then
  284. begin
  285. totaltime:=getrealtime-starttime;
  286. if totaltime<0 then
  287. totaltime:=totaltime+3600.0*24.0;
  288. if round(frac(totaltime)*10) >= 10 then
  289. totaltime:=trunc(totaltime) + 1;
  290. timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
  291. if status.codesize<>aword(-1) then
  292. linkstr:=', '+tostr(status.codesize)+' ' +MessageStr(general_text_bytes_code)+', '+tostr(status.datasize)+' '+MessageStr(general_text_bytes_data)
  293. else
  294. linkstr:='';
  295. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  296. if (Status.Verbosity and V_Warning = V_Warning) and
  297. (Status.CountWarnings <> 0) then
  298. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  299. if (Status.Verbosity and V_Hint = V_Hint) and
  300. (Status.CountHints <> 0) then
  301. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  302. if (Status.Verbosity and V_Note = V_Note) and
  303. (Status.CountNotes <> 0) then
  304. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  305. end;
  306. finally
  307. { no message possible after this !! }
  308. DoneCompiler;
  309. SetExceptionMask(ExceptionMask);
  310. end;
  311. DoneVerbose;
  312. except
  313. on EControlCAbort do
  314. begin
  315. try
  316. { in case of 50 errors, this could cause another exception,
  317. suppress this exception
  318. }
  319. if assigned(m) then
  320. Message(general_f_compilation_aborted)
  321. else
  322. Message(general_f_compiler_aborted);
  323. except
  324. on ECompilerAbort do
  325. ;
  326. end;
  327. DoneVerbose;
  328. end;
  329. on ECompilerAbort do
  330. begin
  331. try
  332. { in case of 50 errors, this could cause another exception,
  333. suppress this exception
  334. }
  335. {$ifdef DUMP_EXCEPTION_BACKTRACE}
  336. DumpExceptionBackTrace(stderr);
  337. {$endif DUMP_EXCEPTION_BACKTRACE}
  338. Message(general_f_compilation_aborted);
  339. except
  340. on ECompilerAbort do
  341. ;
  342. end;
  343. DoneVerbose;
  344. end;
  345. on ECompilerAbortSilent do
  346. begin
  347. DoneVerbose;
  348. end;
  349. on EOutOfMemory do
  350. begin
  351. try
  352. Message(general_f_no_memory_left);
  353. except
  354. on ECompilerAbort do
  355. ;
  356. end;
  357. DoneVerbose;
  358. end;
  359. on e : EInOutError do
  360. begin
  361. try
  362. Message1(general_f_ioerror,e.message);
  363. except
  364. on ECompilerAbort do
  365. ;
  366. end;
  367. DoneVerbose;
  368. end;
  369. on e : EOSError do
  370. begin
  371. try
  372. Message1(general_f_oserror,e.message);
  373. except
  374. on ECompilerAbort do
  375. ;
  376. end;
  377. DoneVerbose;
  378. end;
  379. on Exception do
  380. begin
  381. { General catchall, normally not used }
  382. try
  383. { in case of 50 errors, this could cause another exception,
  384. suppress this exception
  385. }
  386. if not exception_raised then
  387. begin
  388. exception_raised:=true;
  389. Message(general_e_exception_raised);
  390. end
  391. else
  392. Message(general_f_compilation_aborted);
  393. except
  394. on ECompilerAbort do
  395. ;
  396. end;
  397. DoneVerbose;
  398. Raise;
  399. end;
  400. end;
  401. {$ifdef SHOWUSEDMEM}
  402. hstatus:=GetFPCHeapStatus;
  403. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  404. {$endif SHOWUSEDMEM}
  405. { Set the return value if an error has occurred }
  406. if status.errorcount=0 then
  407. result:=0
  408. else
  409. result:=1;
  410. end;
  411. end.