compiler.pas 8.3 KB

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