compiler.pas 9.7 KB

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