compiler.pas 9.7 KB

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