compiler.pas 9.4 KB

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