2
0

compiler.pas 9.9 KB

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