2
0

compiler.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 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. {$if defined(unix) and (FPC_FULLVERSION>20700)}
  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. if round(frac(totaltime)*10) >= 10 then
  245. totaltime:=trunc(totaltime) + 1;
  246. timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
  247. if status.codesize<>aword(-1) then
  248. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  249. else
  250. linkstr:='';
  251. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  252. if (Status.Verbosity and V_Warning = V_Warning) and
  253. (Status.CountWarnings <> 0) then
  254. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  255. if (Status.Verbosity and V_Hint = V_Hint) and
  256. (Status.CountHints <> 0) then
  257. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  258. if (Status.Verbosity and V_Note = V_Note) and
  259. (Status.CountNotes <> 0) then
  260. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  261. end;
  262. finally
  263. { no message possible after this !! }
  264. DoneCompiler;
  265. SetExceptionMask(ExceptionMask);
  266. end;
  267. DoneVerbose;
  268. except
  269. on EControlCAbort do
  270. begin
  271. try
  272. { in case of 50 errors, this could cause another exception,
  273. suppress this exception
  274. }
  275. Message(general_f_compilation_aborted);
  276. except
  277. on ECompilerAbort do
  278. ;
  279. end;
  280. DoneVerbose;
  281. end;
  282. on ECompilerAbort do
  283. begin
  284. try
  285. { in case of 50 errors, this could cause another exception,
  286. suppress this exception
  287. }
  288. Message(general_f_compilation_aborted);
  289. except
  290. on ECompilerAbort do
  291. ;
  292. end;
  293. DoneVerbose;
  294. end;
  295. on ECompilerAbortSilent do
  296. begin
  297. DoneVerbose;
  298. end;
  299. on EOutOfMemory do
  300. begin
  301. try
  302. Message(general_f_no_memory_left);
  303. except
  304. on ECompilerAbort do
  305. ;
  306. end;
  307. DoneVerbose;
  308. end;
  309. on e : EInOutError do
  310. begin
  311. try
  312. Message1(general_f_ioerror,e.message);
  313. except
  314. on ECompilerAbort do
  315. ;
  316. end;
  317. DoneVerbose;
  318. end;
  319. on e : EOSError do
  320. begin
  321. try
  322. Message1(general_f_oserror,e.message);
  323. except
  324. on ECompilerAbort do
  325. ;
  326. end;
  327. DoneVerbose;
  328. end;
  329. on Exception do
  330. begin
  331. { General catchall, normally not used }
  332. try
  333. { in case of 50 errors, this could cause another exception,
  334. suppress this exception
  335. }
  336. Message(general_f_compilation_aborted);
  337. except
  338. on ECompilerAbort do
  339. ;
  340. end;
  341. DoneVerbose;
  342. Raise;
  343. end;
  344. end;
  345. {$ifdef SHOWUSEDMEM}
  346. hstatus:=GetFPCHeapStatus;
  347. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  348. {$endif SHOWUSEDMEM}
  349. { Set the return value if an error has occurred }
  350. if status.errorcount=0 then
  351. result:=0
  352. else
  353. result:=1;
  354. end;
  355. end.