compiler.pas 8.2 KB

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