compiler.pas 9.0 KB

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