compiler.pas 9.2 KB

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