2
0

compiler.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 writecmdstrlist(w:longint;l:TCmdStrList);
  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. GetLocalTime(startsystime);
  229. starttime := getrealtime(startsystime);
  230. { Initialize the compiler }
  231. InitCompiler(cmd);
  232. { show some info }
  233. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  234. Message1(general_d_sourceos,source_info.name);
  235. Message1(general_i_targetos,target_info.name);
  236. Message1(general_t_exepath,exepath);
  237. WriteCmdStrList(general_t_unitpath,unitsearchpath);
  238. WriteCmdStrList(general_t_includepath,includesearchpath);
  239. WriteCmdStrList(general_t_librarypath,librarysearchpath);
  240. WriteCmdStrList(general_t_objectpath,objectsearchpath);
  241. WriteCmdStrList(general_t_unitscope,namespacelist);
  242. { Compile the program }
  243. {$ifdef PREPROCWRITE}
  244. if parapreprocess then
  245. parser.preprocess(inputfilepath+inputfilename)
  246. else
  247. {$endif PREPROCWRITE}
  248. parser.compile(inputfilepath+inputfilename);
  249. { Show statistics }
  250. if status.errorcount=0 then
  251. begin
  252. totaltime:=getrealtime-starttime;
  253. if totaltime<0 then
  254. totaltime:=totaltime+3600.0*24.0;
  255. if round(frac(totaltime)*10) >= 10 then
  256. totaltime:=trunc(totaltime) + 1;
  257. timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
  258. if status.codesize<>aword(-1) then
  259. linkstr:=', '+tostr(status.codesize)+' ' +MessageStr(general_text_bytes_code)+', '+tostr(status.datasize)+' '+MessageStr(general_text_bytes_data)
  260. else
  261. linkstr:='';
  262. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  263. if (Status.Verbosity and V_Warning = V_Warning) and
  264. (Status.CountWarnings <> 0) then
  265. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  266. if (Status.Verbosity and V_Hint = V_Hint) and
  267. (Status.CountHints <> 0) then
  268. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  269. if (Status.Verbosity and V_Note = V_Note) and
  270. (Status.CountNotes <> 0) then
  271. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  272. end;
  273. finally
  274. { no message possible after this !! }
  275. DoneCompiler;
  276. SetExceptionMask(ExceptionMask);
  277. end;
  278. DoneVerbose;
  279. except
  280. on EControlCAbort do
  281. begin
  282. try
  283. { in case of 50 errors, this could cause another exception,
  284. suppress this exception
  285. }
  286. Message(general_f_compilation_aborted);
  287. except
  288. on ECompilerAbort do
  289. ;
  290. end;
  291. DoneVerbose;
  292. end;
  293. on ECompilerAbort 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 ECompilerAbortSilent do
  307. begin
  308. DoneVerbose;
  309. end;
  310. on EOutOfMemory do
  311. begin
  312. try
  313. Message(general_f_no_memory_left);
  314. except
  315. on ECompilerAbort do
  316. ;
  317. end;
  318. DoneVerbose;
  319. end;
  320. on e : EInOutError do
  321. begin
  322. try
  323. Message1(general_f_ioerror,e.message);
  324. except
  325. on ECompilerAbort do
  326. ;
  327. end;
  328. DoneVerbose;
  329. end;
  330. on e : EOSError do
  331. begin
  332. try
  333. Message1(general_f_oserror,e.message);
  334. except
  335. on ECompilerAbort do
  336. ;
  337. end;
  338. DoneVerbose;
  339. end;
  340. on Exception do
  341. begin
  342. { General catchall, normally not used }
  343. try
  344. { in case of 50 errors, this could cause another exception,
  345. suppress this exception
  346. }
  347. if not exception_raised then
  348. begin
  349. exception_raised:=true;
  350. Message(general_e_exception_raised);
  351. end
  352. else
  353. Message(general_f_compilation_aborted);
  354. except
  355. on ECompilerAbort do
  356. ;
  357. end;
  358. DoneVerbose;
  359. Raise;
  360. end;
  361. end;
  362. {$ifdef SHOWUSEDMEM}
  363. hstatus:=GetFPCHeapStatus;
  364. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  365. {$endif SHOWUSEDMEM}
  366. { Set the return value if an error has occurred }
  367. if status.errorcount=0 then
  368. result:=0
  369. else
  370. result:=1;
  371. end;
  372. end.